【记得加php标签】
接端口
//简单工厂对方增加数据类型就得修改工厂,对修改没有封闭违反开闭原则
interface db{
function conn();
}
interface Factory{
function createDB();//工厂接口类
}
//服务器端开发(不知道将会被谁调用)
class dbmysql implements db{//实现
public function conn(){
echo ‘连上了Mysql’;
}
}
//服务端面向接口
class dbsqlite implements db{
public function conn(){
//小型数据库
echo ‘连上了dbsqlite’;
}
}
class dboracle implements db{
public function conn()
{
echo ‘连接上oracle’;
}
}
class mysqlFactory implements Factory{
//实现啦
public function createDB(){
return new dbmysql();
}
}
class sqliteFactory implements Factory{
public function createDB(){
return new dbsqlite();
}
}
class oracleFaction implements Factory {
public function createDB(){
return new dboracle();
}
}
//–客户端开始
$ fact=new mysqlFactory();
$ mysql=$ fact->createDB();
$ mysql->conn();
$ fact=new sqliteFactory();
$ mysql=$ fact->createDB();
$mysql->conn();
$ fact=new oracleFactory();
$ mysql=$ fact->createDB();
$ mysql->conn();