php設(shè)計(jì)模式之簡(jiǎn)單工廠(chǎng)模式詳解
本文以實(shí)例形式較為詳細(xì)的介紹了PHP設(shè)計(jì)模式的簡(jiǎn)單工廠(chǎng)模式,對(duì)于進(jìn)行PHP程序設(shè)計(jì)來(lái)說(shuō)有很好的借鑒作用。具體如下:
一、概念
簡(jiǎn)單工廠(chǎng)模式 【靜態(tài)工廠(chǎng)方法模式】(Static Factory Method)
是類(lèi)的創(chuàng)建模式
工廠(chǎng)模式的幾種形態(tài):
1、簡(jiǎn)單工廠(chǎng)模式(Simple Factory)又叫做 靜態(tài)工廠(chǎng)方法模式(Static Factory Method)
2、工廠(chǎng)方法模式(Factory Method)又叫做 多態(tài)性工廠(chǎng)模式(Polymorphic Factory)
3、抽象工廠(chǎng)模式(Abstract Factory)又叫做 工具箱模式(ToolKit)
二、配圖分析:
三、代碼實(shí)例
該實(shí)例代碼經(jīng)過(guò)測(cè)試可以運(yùn)行,具體如下:
<?php /** * 一個(gè)事例 * * 一個(gè)農(nóng)場(chǎng),要向市場(chǎng)銷(xiāo)售水果 * 農(nóng)場(chǎng)里有三種水果 蘋(píng)果、葡萄 * 我們?cè)O(shè)想:1、水果有多種屬性,每個(gè)屬性都有不同,但是,他們有共同的地方 | 生長(zhǎng)、種植、收貨、吃 * 2、將來(lái)有可能會(huì)增加新的水果、我們需要定義一個(gè)接口來(lái)規(guī)范他們必須實(shí)現(xiàn)的方法 * 3、我們需要獲取某個(gè)水果的類(lèi),要從農(nóng)場(chǎng)主那里去獲取某個(gè)水果的實(shí)例,來(lái)知道如何生長(zhǎng)、種植、收貨、吃 */ /** * 虛擬產(chǎn)品接口類(lèi) * 定義好需要實(shí)現(xiàn)的方法 */ interface fruit{ /** * 生長(zhǎng) */ public function grow(); /** * 種植 */ public function plant(); /** * 收獲 */ public function harvest(); /** * 吃 */ public function eat(); } /** * 定義具體產(chǎn)品類(lèi) 蘋(píng)果 * 首先,我們要實(shí)現(xiàn)所繼承的接口所定義的方法 * 然后定義蘋(píng)果所特有的屬性,以及方法 */ class apple implements fruit{ //蘋(píng)果樹(shù)有年齡 private $treeAge; //蘋(píng)果有顏色 private $color; public function grow(){ echo "grape grow"; } public function plant(){ echo "grape plant"; } public function harvest(){ echo "grape harvest"; } public function eat(){ echo "grape eat"; } //取蘋(píng)果樹(shù)的年齡 public function getTreeAge(){ return $this->treeAge; } //設(shè)置蘋(píng)果樹(shù)的年齡 public function setTreeAge($age){ $this->treeAge = $age; return trie; } } /** * 定義具體產(chǎn)品類(lèi) 葡萄 * 首先,我們要實(shí)現(xiàn)所繼承的接口所定義的方法 * 然后定義葡萄所特有的屬性,以及方法 */ class grape implements fruit{ //葡萄是否有籽 private $seedLess; public function grow(){ echo "apple grow"; } public function plant(){ echo "apple plant"; } public function harvest(){ echo "apple harvest"; } public function eat(){ echo "apple eat"; } //有無(wú)籽取值 public function getSeedLess(){ return $this->seedLess; } //設(shè)置有籽無(wú)籽 public function setSeedLess($seed){ $this->seedLess = $seed; return true; } } /** *農(nóng)場(chǎng)主類(lèi) 用來(lái)獲取實(shí)例化的水果 * */ class farmer{ //定義個(gè)靜態(tài)工廠(chǎng)方法 public static function factory($fruitName){ switch ($fruitName) { case 'apple': return new apple(); break; case 'grape': return new grape(); break; default: throw new badFruitException("Error no the fruit", 1); break; } } } class badFruitException extends Exception{ public $msg; public $errType; public function __construct($msg = '' , $errType = 1){ $this->msg = $msg; $this->errType = $errType; } } /** * 獲取水果實(shí)例化的方法 */ try{ $appleInstance = farmer::factory('apple'); var_dump($appleInstance); }catch(badFruitException $err){ echo $err->msg . "_______" . $err->errType; }
希望本文所述實(shí)例對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP通過(guò)session id 實(shí)現(xiàn)session共享和登錄驗(yàn)證的代碼
PHP通過(guò)session id 實(shí)現(xiàn)session共享和登錄驗(yàn)證的代碼,需要的朋友可以參考下2012-06-06關(guān)于PHP結(jié)束標(biāo)簽的使用細(xì)節(jié)探討及聯(lián)想
PHP解析文件時(shí)會(huì)尋找開(kāi)始?php和結(jié)束標(biāo)記?,標(biāo)記告訴PHP開(kāi)始和停止解釋其中的代碼,接下來(lái)將詳細(xì)介紹下PHP結(jié)束標(biāo)簽的使用細(xì)節(jié)感興趣的你可以參考下本文或許可以幫助到你2013-03-03PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個(gè)函數(shù)介紹
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個(gè)函數(shù)介紹,需要的朋友可以參考下2014-05-05php計(jì)算數(shù)組相同值出現(xiàn)次數(shù)的代碼(array_count_values)
這篇文章主要介紹了php計(jì)算數(shù)組相同值出現(xiàn)次數(shù)的代碼,需要的朋友可以參考下2015-01-01PHP運(yùn)行出現(xiàn)Notice : Use of undefined constant 的完美解決方案分享
今天修改公司的網(wǎng)站,提示Notice : Use of undefined constant ,通過(guò)下面的方法解決了,最好是error_reporting(0);不需要更改配置2012-03-03