当前位置: 移动技术网 > IT编程>开发语言>PHP > php设计模式 Factory(工厂模式)

php设计模式 Factory(工厂模式)

2019年05月25日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<?php
/**
* 工厂方法模式
*
* 定义一个用于创建对象的接口,让子类决定将哪一个类实例化,使用一个类的实例化延迟到其子类
*/

/*
class dbfactory
{
public static function create($type)
{
swtich($type)
{
case "mysql":
return new mysqldb(); break;
case "postgre":
return new postgredb(); break;
case "mssql":
return new mssqldb(); break;
}
}
}
*/
class dbfactory
{
public static function create($type)
{
$class = $type."db";
return new $class;
}
}

interface db
{
public function connect();
public function exec();
}

class mysqldb implements db
{
public function __construct() {
echo "mysql db<br/>";
}

public function connect() {
}

public function exec() {
}
}

class postgredb implements db
{
public function __construct() {
echo "postgre db<br/>";
}

public function connect() {
}

public function exec() {
}
}

class mssqldb implements db
{
public function __construct() {
echo "mssql db<br/>";
}

public function connect() {
}
public function exec() {
}
}

$omysql = dbfactory::create("mysql");
$opostgre = dbfactory::create("postgre");
$omssql = dbfactory::create("mssql");

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网