标签:特定 factor mysq use nts pac ase fence get
/** * 工厂方法或者类生成对象,而不是在代码中直接new * * 修改类名的时候,不需要每一个实例化语句都修改 * 只需要修改对应的工厂方法 * * Class Factory * @package regina */ class Factory{ static function createDatabase(){ $db = Database::getInstance(); return $db; } }
/** * 单例模式,节省资源,一个类只能实例化一个对象 * * 使某个类的对象仅允许创建一个 * * 为了实现单例模式,要私有化构造函数和clone魔术方法 * Class Database * @package regina */ class Database{ protected static $db; private function __construct(){ } private function __clone(){ } static function getInstance(){ if (self::$db){ return self::$db; } self::$db = new Database(); return self::$db; } }
/** * 把new出来的对象保存在静态数值中,以便全局共享 * 全局共享和交换对象 * Class Register * @package regina */ class Register{ protected static $array; static function set($name, $Obj){ echo ‘Register set run....‘; self::$array[$name] = $Obj; } static function get($name){ echo ‘Register get run....‘; return self::$array[$name]; } function _unset($name){ echo ‘Register unset run....‘; unset(self::$array[$name]); } }
/** * 将各种截然不同的函数接口封装成统一的API * Interface IDatabase * @package regina */ interface IDatabase{ function connect($host, $username, $password, $dbname); function query(); function close(); } class Mysqli implements Database{ protected $conn; function connect($host, $username, $password, $dbname){ echo ‘Mysqli connect run...‘; } function query(){ echo ‘Mysqli query run...‘; } function close(){ echo ‘Mysqli close run...‘; } } class Mysql implements Database{ function connect($host, $username, $password, $dbname){ echo ‘Mysql connect run...‘; } function query(){ echo ‘Mysql query run...‘; } function close(){ echo ‘Mysql close run...‘; } }
将一组特定的行为和算法封装成类,以适应默写上下文的定义。
标签:特定 factor mysq use nts pac ase fence get
原文地址:http://www.cnblogs.com/alihenaixiao/p/6014424.html