PHP工厂模式
<?php
interface Db
{
public function conn();
}
interface Factory
{
public function createDb();
}
class Mysql implements Db
{
public function conn()
{
echo "连接MySql成功<br>";
}
}
class Sqlite implements Db
{
public function conn()
{
echo "连接Sqlite成功<br>";
}
}
class mysqlFactory implements Factory
{
public function createDb() {
return new Mysql();
}
}
class sqliteFactory implements Factory
{
public function createDb()
{
return new Sqlite();
}
}
$mysqlFactory = new mysqlFactory();
$db=$mysqlFactory->createDb();
$db->conn();
$sqliteFactory = new sqliteFactory();
$db=$sqliteFactory->createDb();
$db->conn();