PHP設計模式之:原廠模式

來源:互聯網
上載者:User
原廠模式:

由工廠類根據參數來決定建立出哪一種產品類的執行個體;

工廠類是指包含了一個專門用來建立其他對象的方法的類。所謂按需分配,傳入參數進行選擇,返回具體的類。原廠模式的最主要作用就是對象建立的封裝、簡化建立對象操作。

簡單的說,就是調用工廠類的一個方法(傳入參數)來得到需要的類;

代碼實現:

樣本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());



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.