您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP设计模式之:工厂模式

2025/11/22 11:35:25发布24次查看
工厂模式:
由工厂类根据参数来决定创建出哪一种产品类的实例;
工厂类是指包含了一个专门用来创建其他对象的方法的类。所谓按需分配,传入参数进行选择,返回具体的类。工厂模式的最主要作用就是对象创建的封装、简化创建对象操作。
简单的说,就是调用工厂类的一个方法(传入参数)来得到需要的类;
代码实现:
示例1(最基本的工厂类):
<?php class myobject { public function __construct(){} public function test(){ return '测试'; } } class myfactory { public static function factory(){ //返回对象的实例 return new myobject(); } } //调用工厂类myfactory中的静态方法,获取类myobject的实例 $myobject=myfactory::factory(); echo $myobject->test();
示例2:
<?php //简单工厂模式 /1* * 定义运算类 */ abstract class operation { protected $_numbera = 0; protected $_numberb = 0; protected $_result = 0; public function __construct($a,$b){ $this->_numbera = $a; $this->_numberb = $b; } public function setnumber($a,$b){ $this->_numbera = $a; $this->_numberb = $b; } /1* protected function clearresult(){ $this->_result = 0; } */ public function clearresult(){ $this->_result = 0; } //抽象方法无方法体 abstract protected function getresult(); } //继承一个抽象类的时候,子类必须实现抽象类中的所有抽象方法; //另外,这些方法的可见性 必须和抽象类中一样(或者更为宽松) class operationadd extends operation { public function getresult(){ $this->_result=$this->_numbera + $this->_numberb; return $this->_result; } } class operationsub extends operation { public function getresult(){ $this->_result=$this->_numbera - $this->_numberb; return $this->_result; } } class operationmul extends operation { public function getresult(){ $this->_result=$this->_numbera * $this->_numberb; return $this->_result; } } class operationdiv extends operation { public function getresult(){ $this->_result=$this->_numbera / $this->_numberb; return $this->_result; } } class operationfactory { //创建保存实例的静态成员变量 private static $obj; //创建访问实例的公共的静态方法 public static function createoperation($type,$a,$b){ switch($type){ case '+': self::$obj = new operationadd($a,$b); break; case '-': self::$obj = new operationsub($a,$b); break; case '*': self::$obj = new operationmul($a,$b); break; case '/': self::$obj = new operationdiv($a,$b); break; } return self::$obj; } } //$obj = operationfactory::createoperation('+'); //$obj->setnumber(4,4); $obj = operationfactory::createoperation('*',5,6); echo $obj->getresult(); /1* echo '<br>'; $obj->clearresult(); echo '<br>'; echo $obj->_result; */
示例3:
<?php //抽象工厂 //青铜会员的打折商品 class bronzerebatecommodity { //描述 public $desc = '青铜会员的打折商品'; } //白银会员的打折商品 class silverrebatecommodity { public $desc = '白银会员的打折商品'; } //青铜会员的推荐商品 class bronzecommendatorycommodity { public $desc = '青铜会员的推荐商品'; } //白银会员的推荐商品 class silvercommendatorycommodity { public $desc = '白银会员的推荐商品'; } //各个工厂的接口 interface concretefactory { //生产对象的方法 public function create($what); } //青铜工厂 class bronzefactory implements concretefactory { //生产对象的方法 public function create($what){ $productname = 'bronze'.$what.'commodity'; return new $productname; } } //白银工厂 class silverfactory implements concretefactory { //生产对象的方法 public function create($what){ $productname = 'silver'.$what.'commodity'; return new $productname; } } //调度中心 class centerfactory { //获取工厂的方法 public function getfactory($what){ $factoryname = $what.'factory'; return new $factoryname; } //获取工厂的静态方法 public static function getfactory2($what){ $factoryname = $what.'factory'; return new $factoryname; } } //实例化调度中心 $center = new centerfactory(); //获得一个白银工厂 $factory = $center->getfactory('silver'); //让白银工厂制造一个推荐商品 $product = $factory->create('commendatory'); //得到白银会员的推荐商品 echo $product->desc.'<br>'; //获得一个青铜工厂 $factory2 = centerfactory::getfactory2('bronze'); //让青铜工厂制造一个打折商品 $product2 = $factory2->create('rebate'); //得到青铜会员的推荐商品 echo $product2->desc;
示例4:
<?php //使用工厂类解析图像文件 interface iimage { function getwidth(); function getheight(); function getdata(); } class image_png implements iimage { protected $_width,$_height,$_data; public function __construct($file){ $this->_file = $file; $this->_parse(); } private function _parse(){ //完成png格式的解析工作 //并填充$_width,$_height和$_data $this->_data = getimagesize($this->_file); list($this->_width,$this->_height)=$this->_data; } public function getwidth(){ return $this->_width; } public function getheight(){ return $this->_height; } public function getdata(){ return $this->_data; } } class image_jpeg implements iimage { protected $_width,$_height,$_data; public function __construct($file){ $this->_file = $file; $this->_parse(); } private function _parse(){ //完成jpeg格式的解析工作 //并填充$_width,$_height和$_data //$this->_width = imagesx($this->_file); //$this->_height = imagesy($this->_file); $this->_data = getimagesize($this->_file); list($this->_width,$this->_height)=$this->_data; } public function getwidth(){ return $this->_width; } public function getheight(){ return $this->_height; } public function getdata(){ return $this->_data; } } //工厂类 class imagefactory { public static function factory($file){ $filename = pathinfo($file); switch(strtolower($filename['extension'])){ case 'jpg': $return = new image_jpeg($file); break; case 'png': $return = new image_png($file); break; default: echo '图片类型不正确'; break; } if($return instanceof iimage){ return $return; }else{ echo '出错了'; exit(); } } } $image = imagefactory::factory('images/11.jpg'); var_dump($image->getwidth()); echo '<br>'; print_r($image->getheight()); echo '<br>'; print_r($image->getdata());
更多php设计模式之:工厂模式。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product