简单工厂
//公共接口
interface db(){
function conn(){
}
}
//服务端
class dbmysql implements db {
public function conn(){
echo '连接上了mysql';
}
}
class dbsqlite implements db {
public function conn(){
echo '连接上了sqlite';
}
}
class Factory {
public static function createDB($type){
if ($type == 'mysql') {
return new dbmysql();
} else if ($type == 'sqlite') {
return new dbsqlite();
} else {
throw new \Exception('db type error');
}
}
}
//客户端
$db = Factory::createDB('mysql');
$db->conn();
$db = Factory::createDB('sqlite');
$db->conn();
面向对象设计法则,开闭原则:对于修改是封闭的,对于扩展是开放的