php設(shè)計(jì)模式介紹之值對(duì)象模式第4/5頁(yè)
更新時(shí)間:2008年04月13日 22:10:17 作者:
在所有的最簡(jiǎn)單的程序中,大多數(shù)對(duì)象都有一個(gè)標(biāo)識(shí),一個(gè)重要的商業(yè)應(yīng)用對(duì)象,例如一個(gè)Customer或者一個(gè)SKU,有一個(gè)或者更多的屬性---id,name,email地址,這樣可以把它從同一個(gè)類的其他實(shí)例區(qū)分開(kāi)來(lái)。此外,對(duì)象有一個(gè)恒定的標(biāo)識(shí):它是貫穿于整個(gè)應(yīng)用程序的一個(gè)唯一的標(biāo)識(shí),對(duì)于程序員來(lái)說(shuō),”customer A”在任何地方就是”customer A”,并且只要你的程序在持續(xù)運(yùn)行時(shí)"customer A"仍然是"customer A"。 但是一個(gè)對(duì)象不需要有一個(gè)標(biāo)識(shí)。有些對(duì)象僅僅是為了描述其他對(duì)象的屬性。
另一個(gè)重要的概念是對(duì)象Monopoly中的租金支付。讓我們首先寫(xiě)一個(gè)測(cè)試實(shí)例(測(cè)試引導(dǎo)開(kāi)發(fā))。下面的代碼希望用來(lái)實(shí)現(xiàn)既定的目標(biāo)。
function TestRent() {
$game = new Monopoly;
$player1 = new Player(‘Madeline');
$player2 = new Player(‘Caleb');
$this->assertEqual(1500, $player1->getBalance());
$this->assertEqual(1500, $player2->getBalance());
$game->payRent($player1, $player2, new Dollar(26));
$this->assertEqual(1474, $player1->getBalance());
$this->assertEqual(1526, $player2->getBalance());
}
根據(jù)這個(gè)測(cè)試代碼,我們需要在Monopoly對(duì)象中增加payRent()的方法函數(shù)來(lái)實(shí)現(xiàn)一個(gè)Player對(duì)象去支付租金給另一個(gè)Player對(duì)象.
Class Monopoly {
// ...
/**
* pay rent from one player to another
* @param Player $from the player paying rent
* @param Player $to the player collecting rent
* @param Dollar $rent the amount of the rent
* @return void
*/
public function payRent($from, $to, $rent) {
$to->collect($from->pay($rent));
}
}
payRent()方法函數(shù)實(shí)現(xiàn)了兩個(gè)player對(duì)象之間($from和$to)的租金支付。方法函數(shù)Player::collect()已經(jīng)被定義了,但是Player::pay()必須被添加進(jìn)去,以便實(shí)例$from通過(guò)pay()方法支付一定的Dollar數(shù)額$to對(duì)象中。首先我們定義Player::pay()為:
class Player {
// ...
public function pay($amount) {
$this->savings = $this->savings->add(-1 * $amount);
}
}
但是,我們發(fā)現(xiàn)在PHP中你不能用一個(gè)數(shù)字乘以一個(gè)對(duì)象(不像其他語(yǔ)言,PHP不允許重載操作符,以便構(gòu)造函數(shù)進(jìn)行運(yùn)算)。所以,我們通過(guò)添加一個(gè)debit()方法函數(shù)實(shí)現(xiàn)Dollar對(duì)象的減的操作。
class Dollar {
protected $amount;
public function __construct($amount=0) {
$this->amount = (float)$amount;
}
public function getAmount() {
return $this->amount;
}
public function add($dollar) {
return new Dollar($this->amount + $dollar->getAmount());
}
public function debit($dollar) {
return new Dollar($this->amount - $dollar->getAmount());
}
}
引入Dollar::debit()后,Player::pay()函數(shù)的操作依然是很簡(jiǎn)單的。
class Player {
// ...
/**
* make a payment
* @param Dollar $amount the amount to pay
* @return Dollar the amount payed
*/
public function pay($amount) {
$this->savings = $this->savings->debit($amount);
return $amount;
}
}
Player::pay()方法返回支付金額的$amount對(duì)象,所以Monopoly::payRent()中的語(yǔ)句$to->collect($from->pay($rent))的用法是沒(méi)有問(wèn)題的。這樣做的話,如果將來(lái)你添加新的“商業(yè)邏輯”用來(lái)限制一個(gè)player不能支付比他現(xiàn)有的余額還多得金額。(在這種情況下,將返回與player的賬戶余額相同的數(shù)值。同時(shí),也可以調(diào)用一個(gè)“破產(chǎn)異常處理”來(lái)計(jì)算不足的金額,并進(jìn)行相關(guān)處理。對(duì)象$to仍然從對(duì)象$from中取得$from能夠給予的金額。)
注:術(shù)語(yǔ)------商業(yè)邏輯
在一個(gè)游戲平臺(tái)的例子上提及的“商業(yè)邏輯”似乎無(wú)法理解。這里的商業(yè)的意思并不是指正常公司的商業(yè)運(yùn)作,而是指因?yàn)樘厥鈶?yīng)用領(lǐng)域需要的概念。請(qǐng)把它認(rèn)知為 “一個(gè)直接的任務(wù)或目標(biāo)”,而不是“這里面存在的商業(yè)操作”。
所以,既然目前我們討論的是一個(gè)Monopoly,那么這里的 “商業(yè)邏輯”蘊(yùn)含的意思就是針對(duì)一個(gè)游戲的規(guī)則而說(shuō)的。
您可能感興趣的文章:
- php設(shè)計(jì)模式 Observer(觀察者模式)
- php設(shè)計(jì)模式 Singleton(單例模式)
- php設(shè)計(jì)模式 Command(命令模式)
- php設(shè)計(jì)模式 Composite (組合模式)
- php設(shè)計(jì)模式 Bridge (橋接模式)
- php設(shè)計(jì)模式 Chain Of Responsibility (職責(zé)鏈模式)
- php設(shè)計(jì)模式 Mediator (中介者模式)
- php設(shè)計(jì)模式 Prototype (原型模式)代碼
- php設(shè)計(jì)模式 Command(命令模式)
- 介紹php設(shè)計(jì)模式中的工廠模式
- php設(shè)計(jì)模式介紹之編程慣用法
- php設(shè)計(jì)模式 Strategy(策略模式)
- php設(shè)計(jì)模式 FlyWeight (享元模式)
- php設(shè)計(jì)模式 State (狀態(tài)模式)
- php設(shè)計(jì)模式 Proxy (代理模式)
- php設(shè)計(jì)模式 Template (模板模式)
相關(guān)文章
簡(jiǎn)單介紹win7下搭建apache+php+mysql開(kāi)發(fā)環(huán)境
這里給大家介紹的是Win7下搭建“PHP+Apache+MySql”網(wǎng)站運(yùn)行環(huán)境詳細(xì)方法步驟,十分的細(xì)致全面,有需要的小伙伴可以參考下。2015-08-08在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的運(yùn)行環(huán)境
這篇文章主要介紹了在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的運(yùn)行環(huán)境,需要的朋友可以參考下2016-04-04php無(wú)限級(jí)分類實(shí)現(xiàn)方法分析
這篇文章主要介紹了php無(wú)限級(jí)分類實(shí)現(xiàn)方法,結(jié)合2個(gè)簡(jiǎn)單實(shí)例形式分析了php通過(guò)遞歸與普通算法實(shí)現(xiàn)無(wú)限級(jí)分類的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10php實(shí)現(xiàn)Linux服務(wù)器木馬排查及加固功能
這篇文章主要介紹了php實(shí)現(xiàn)Linux服務(wù)器木馬排查及加固功能,本文給出了根據(jù)特征碼查找、搜索最近被修改的文件、修改php.ini、修改nginx.conf等方法,需要的朋友可以參考下2014-12-12php中g(shù)et_object_vars()方法用法實(shí)例
這篇文章主要介紹了php中g(shù)et_object_vars()方法用法,實(shí)例分析了get_object_vars()方法獲取對(duì)象中屬性的使用技巧,需要的朋友可以參考下2015-02-02