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

前端之家收集整理的这篇文章主要介绍了php设计模式 Factory(工厂模式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="78754" class="copybut" id="copybut78754" onclick="doCopy('code78754')"> 代码如下:

<div class="codebody" id="code78754"> @H_404_2@<?PHP @H_4042@/** @H404_2@ 工厂方法模式 @H_404_2@ @H_4042@* 定义一个用于创建对象的接口,让子类决定将哪一个类实例化,使用一个类的实例化延迟到其子类 @H4042@/ / @H4042@class DBFactory @H4042@{ @H4042@public static function create($type) @H4042@{ @H4042@swtich($type) @H4042@{ @H404_2@case "MysqL": @H_404_2@return new MysqLDB(); break; @H_4042@case "Postgre": @H4042@return new PostgreDB(); break; @H404_2@case "Mssql": @H_404_2@return new MssqlDB(); break; @H_4042@} @H4042@} @H4042@} @H4042@*/ @H4042@class DBFactory @H4042@{ @H4042@public static function create($type) @H4042@{ @H4042@$class = $type."DB"; @H4042@return new $class; @H4042@} @H4042@} interface DB @H4042@{ @H4042@public function connect(); @H4042@public function exec(); @H404_2@} class MysqLDB implements DB @H_4042@{ @H404_2@public function construct() { @H_404_2@echo "MysqL db
"; @H_4042@} public function connect() { @H4042@} public function exec() { @H4042@} @H4042@} class PostgreDB implements DB @H4042@{ @H404_2@public function
construct() { @H_4042@echo "Postgre db
"; @H
4042@} public function connect() { @H4042@} public function exec() { @H4042@} @H404_2@} class MssqlDB implements DB @H_4042@{ @H404_2@public function _construct() { @H404_2@echo "mssql db
"; @H_4042@} public function connect() { @H4042@} @H4042@public function exec() { @H4042@} @H404_2@} $oMysqL = DBFactory::create("MysqL"); @H_4042@$oPostgre = DBFactory::create("Postgre"); @H404_2@$oMssql = DBFactory::create("Mssql"); @H_404_2@

猜你在找的PHP相关文章