工厂模式
方便未来扩展,设计成工厂模式。如果已经使用的类内部发生改变,哪不需要在所有的地方都改变,只需要在类工厂类里改变既可,
interface Mysql{
public function connect();
}
class Mysqli2 implements Mysql{
public function connect()
{
// TODO: Implement connect() method.
echo "mysql2";
}
}
class Pdo implements Mysql{
public function connect()
{
// TODO: Implement connect() method.
echo "pdo";
}
}
class MysqlFactory
{
static public function factory($className)
{
return new $className();
}
}
单例模式
class Factory
{
static private $instance;
private function __construct(){}
private function __clone(){}
public function test()
{
echo "单例测试";
}
static public function getInstance()
{
if (!self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
}
|