标签:
<?php
interface DB{
function conn();
}
class MysqlDB implements DB{
public function conn(){
echo "mysql conn";
}
}
class OracleDB implements DB{
public function conn(){
echo "oracle conn";
}
}
interface Factory{
function createDB();
}
class MysqlFactory implements Factory{
public function createDB(){
return new MysqlDB();
}
}
class OracleFactory implements Factory{
public function createDB(){
return new OracleDB();
}
}
//调用
$facroty = new MysqlFactory();
$db = $facroty->createDB();
$db->conn();
$facroty = new OracleFactory();
$db = $facroty->createDB();
$db->conn();
实现了对象修改是封闭,对于扩展是开放。
标签:
原文地址:http://www.cnblogs.com/shenming/p/4216661.html