工厂模式:
class Factroy
{
static function createDatabase(){
$db = new Database();
return $db;
}
}
//不使用工厂模式
$db = new Database();
//使用工厂模式
$db = Factroy ::createDatabase();
单利模式
class Database
{
private static $db;
private function __construct(){ } //构造方法私有化
static function getInstance() //返回静态对象
{
if(self::$db){
return self::$db;
}else{
self::$db = new self();
return self::$db;
}
}
public function test(){
echo 'test';
}
}
$obj = \Common\Database::getInstance();
$obj->test();
注册树模式
class Register{
private static $objects;
//注册到全局树 alias映射 object对象
static function _set($alias,$object){ //注册
}
static function _unset($alias){ /销毁
unset(self::$objects[$alias]);
}
static function get($alias){ //查找
return self::$objects[$alias];
}
}
//然后在工厂类中注册
class Factroy
{
static function createDatabase(){
$db = new Object();
Register::_set('db',$db);
return $db;
}
}