《PHP應(yīng)用:實(shí)例講解PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠模式》要點(diǎn):
本文介紹了PHP應(yīng)用:實(shí)例講解PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠模式,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP教程簡(jiǎn)單工廠模式是類(lèi)的創(chuàng)建模式,又叫做靜態(tài)工廠辦法(Static Factory Method)模式.簡(jiǎn)單工廠模式是由一個(gè)工廠對(duì)象決定創(chuàng)建出那一種產(chǎn)品類(lèi)的實(shí)例.
PHP教程1.工廠模式的幾種形態(tài)
工廠模式專(zhuān)門(mén)負(fù)責(zé)將大量有共同接口的類(lèi)實(shí)例化.工廠模式可以動(dòng)態(tài)決定將哪一個(gè)類(lèi)實(shí)例化,不必事先知道每次要實(shí)例化哪一個(gè)類(lèi).工廠模式有以下幾種形態(tài):
(1)簡(jiǎn)單工廠(Simple Factory)模式,又稱(chēng)靜態(tài)工廠辦法模式(Static Factory Method Pattern).
(2)工廠辦法(Factory Method)模式,又稱(chēng)多態(tài)性工廠(Polymorphic Factory)模式或虛擬構(gòu)造子(Virtual Constructor)模式;
(3)抽象工廠(Abstract Factory)模式,又稱(chēng)工具箱(Kit 或Toolkit)模式.下面就是簡(jiǎn)單工廠模式的簡(jiǎn)略類(lèi)圖.
PHP教程簡(jiǎn)單工廠模式,或稱(chēng)靜態(tài)工廠辦法模式,是不同的工廠辦法模式的一個(gè)特殊實(shí)現(xiàn).在其他文獻(xiàn)中,簡(jiǎn)單工廠往往作為普通工廠模式的一個(gè)特例討論.
學(xué)習(xí)簡(jiǎn)單工廠模式是對(duì)學(xué)習(xí)工廠辦法模式的一個(gè)很好的準(zhǔn)備,也是對(duì)學(xué)習(xí)其他模式,特別是單例模式和多例模式的一個(gè)很好的準(zhǔn)備.
PHP教程2 .簡(jiǎn)單工廠模式的引進(jìn)
PHP教程比如說(shuō)有一個(gè)農(nóng)場(chǎng)公司,專(zhuān)門(mén)向市場(chǎng)銷(xiāo)售各類(lèi)水果.在這個(gè)系統(tǒng)里需要描述下列的水果:
葡萄 Grape
草莓 Strawberry
蘋(píng)果 Apple
水果與其他的植物有很大的不同,就是水果最終是可以采摘食用的.那么一個(gè)自然的作法就是建立一個(gè)各種水果都適用的接口,以便與農(nóng)場(chǎng)里的其他植物區(qū)分開(kāi).如下圖所示.
PHP教程水果接口規(guī)定出所有的水果必須實(shí)現(xiàn)的接口,包括任何水果類(lèi)必須具備的辦法:種植plant(),生長(zhǎng)grow()以及收獲harvest().接口Fruit 的類(lèi)圖如下所示.
PHP教程這個(gè)水果接口的源代碼如下所示.
代碼清單1:接口Fruit 的源代碼
PHP教程
interface Fruit
{
public function grow();
public function harvest();
public function plant();
}
PHP教程Apple 類(lèi)是水果類(lèi)的一種,因此它實(shí)現(xiàn)了水果接口所聲明的所有辦法.另外,由于蘋(píng)果是多年生植物,因此多出一個(gè)treeAge 性質(zhì),描述蘋(píng)果樹(shù)的樹(shù)齡.下面是這個(gè)蘋(píng)果類(lèi)的源代碼.
代碼清單2:類(lèi)Apple 的源代碼
PHP教程
class Apple implements Fruit
{
private $_treeAge;
public function grow()
{
echo "Apple is growing.";
}
public function harvest()
{
echo "Apple has been harvested.";
}
public function plant()
{
echo "Apple has been planted.";
}
public function getTreeAge()
{
return $this->_treeAge;
}
public function setTreeAge($treeAge)
{
$this->_treeAge = (int) $treeAge;
}
}
PHP教程同樣,Grape 類(lèi)是水果類(lèi)的一種,也實(shí)現(xiàn)了Fruit 接口所聲明的所有的辦法.但由于葡萄分有籽和無(wú)籽兩種,因此,比通常的水果多出一個(gè)seedless 性質(zhì),如下圖所示.
PHP教程葡萄類(lèi)的源代碼如下所示.可以看出,Grape 類(lèi)同樣實(shí)現(xiàn)了水果接口,從而是水果類(lèi)型的一種子類(lèi)型.
代碼清單3:類(lèi)Grape 的源代碼
PHP教程
class Grape implements Fruit
{
private $seedless;
public function grow()
{
echo "Grape is growing.";
}
public function harvest()
{
echo "Grape has been harvested.";
}
public function plant()
{
echo "Grape has been planted.";
}
public function getSeedless()
{
return $this->seedless;
}
public function setSeedless($seedless)
{
$this->seedless = (boolean) $seedless;
}
}
PHP教程Strawberry 類(lèi)實(shí)現(xiàn)了Fruit 接口,因此,也是水果類(lèi)型的子類(lèi)型,其源代碼如下所示.
代碼清單4:類(lèi)Strawberry 的源代碼
PHP教程
class Strawberry implements Fruit
{
public function grow()
{
echo "Strawberry is growing.";
}
public function harvest()
{
echo "Strawberry has been harvested.";
}
public function plant()
{
echo "Strawberry has been planted.";
}
}
PHP教程農(nóng)場(chǎng)的園丁也是系統(tǒng)的一部分,自然要由一個(gè)合適的類(lèi)來(lái)代表.這個(gè)類(lèi)就是FruitGardener 類(lèi),其結(jié)構(gòu)由下面的類(lèi)圖描述.
PHP教程FruitGardener 類(lèi)會(huì)根據(jù)客戶(hù)端的要求,創(chuàng)建出不同的水果對(duì)象,比如蘋(píng)果(Apple),葡萄(Grape)或草莓(Strawberry)的實(shí)例.而如果接到不合法的要求,FruitGardener 類(lèi)會(huì)拋出BadFruitException 異常.
園丁類(lèi)的源代碼如下所示.
代碼清單5:FruitGardener 類(lèi)的源代碼
PHP教程
class FruitGardener
{
public static function factory($which)
{
$which = strtolower($which);
if ($which == 'apple') {
return new Apple();
} elseif ($which == 'strawberry') {
return new Strawberry();
} elseif ($which == 'grape') {
return new Grape();
} else {
throw new BadFruitException('Bad fruit request');
}
}
}
PHP教程可以看出,園丁類(lèi)提供了一個(gè)靜態(tài)工廠方法.在客戶(hù)端的調(diào)用下,這個(gè)方法創(chuàng)建客戶(hù)端所需要的水果對(duì)象.如果客戶(hù)端的哀求是系統(tǒng)所不支持的,工廠方法就會(huì)拋出一個(gè)BadFruitException 異常.這個(gè)異常類(lèi)的源代碼如下所示.
代碼清單6:BadFruitException 類(lèi)的源代碼
PHP教程
class BadFruitException extends Exception
{
}
PHP教程在使用時(shí),客戶(hù)端只需調(diào)用FruitGardener 的靜態(tài)辦法factory()即可.請(qǐng)見(jiàn)下面的示意
性客戶(hù)端源代碼.
代碼清單7:怎樣使用異常類(lèi)BadFruitException
PHP教程
try {
FruitGardener::factory('apple');
FruitGardener::factory('grape');
FruitGardener::factory('strawberry');
//...
} catch (BadFruitException $e) {
//...
}
PHP教程這樣,農(nóng)場(chǎng)一定會(huì)百果豐收啦!
PHP教程3.使用簡(jiǎn)單工廠模式設(shè)計(jì)一個(gè)“面向?qū)ο蟮摹庇?jì)算器
PHP教程
/**
* 面向?qū)ο笥?jì)算器
* 思路:
* 1、面向?qū)ο蟮幕?封裝、繼承、多太
* 2、父類(lèi)公用類(lèi)
* 3、各種運(yùn)算類(lèi)
*/
/**
* 基類(lèi),運(yùn)算類(lèi)
* 只提供基本數(shù)據(jù),不參與運(yùn)算
*/
class Operation {
// 第一個(gè)數(shù)
public $first_num = 0;
// 第二個(gè)數(shù)
public $second_num = 0;
/**
* 獲取結(jié)果,其他類(lèi)覆蓋此辦法
* @return double $result
*/
public function getResult() {
$result = 0.00;
return $result;
}
}
/**
* 加法類(lèi)
*/
class OperationAdd extends Operation {
/**
* 覆蓋父類(lèi),實(shí)現(xiàn)加法算法
*/
public function getResult() {
$result = 0;
return $this->first_num + $this->second_num;
}
}
/**
* 減法類(lèi)
*
*/
class OperationSub extends Operation {
/**
* 覆蓋父類(lèi),實(shí)現(xiàn)加法算法
*/
public function getResult() {
$result = 0;
return $this->first_num - $this->second_num;
}
}
/**
* 乘法類(lèi)
*
*/
class OperationMul extends Operation {
/**
* 覆蓋父類(lèi),實(shí)現(xiàn)加法算法
*/
public function getResult() {
$result = 0;
return $this->first_num * $this->second_num;
}
}
/**
* 除類(lèi)
*
*/
class OperationDiv extends Operation {
/**
* 覆蓋父類(lèi),實(shí)現(xiàn)加法算法
*/
public function getResult() {
$result = 0;
if ($this->second_num == 0) {
throw new Exception('除法操作第二個(gè)參數(shù)不能為零!');
return 0;
}
return $this->first_num / $this->second_num;
}
}
/**
* 工廠類(lèi)
*/
class OperationFactory {
/**
* 工廠函數(shù)
* @param string $operation
* @return object
*/
public function createOperation($operation) {
$oper = null;
switch($operation) {
case '+':
$oper = new OperationAdd();
break;
case '-':
$oper = new OperationSub();
break;
case '*':
$oper = new OperationMul();
break;
case '/':
$oper = new OperationDiv();
break;
default:
return 0;
}
return $oper;
}
}
$operation = new OperationFactory();
$oper = $operation->createOperation('/');
$oper->first_num = 10;
$oper->second_num = 20;
var_dump($oper->getResult());
PHP教程
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP應(yīng)用:實(shí)例講解PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠模式》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/7496.html