<?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();
实现了对象修改是封闭,对于扩展是开放。
时间: 2024-11-04 22:06:03