日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

《PHP設(shè)計模式介紹》第三章 工廠模式(7)_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:《PHP設(shè)計模式介紹》第二章 值對象模式
在所有的最簡單的程序中,大多數(shù)對象都有一個標識,一個重要的商業(yè)應用對象,例如一個Customer或者一個SKU,有一個或者更多的屬性---id,name,email地址,這樣可以把它從同一個類的其他實例區(qū)分開

下面一個測試代碼就是測試 PropertyInfo 類的:

function testPropertyInfo() {
$list = array(‘type’,’price’,’color’,’rent’);
$this->assertIsA(
$testprop = new PropertyInfo($list), ‘PropertyInfo’);
foreach($list as $prop) {
$this->assertEqual($prop, $testprop->$prop);
}
}

這個測試證明:每個PropertyInfo類都有四個公共屬性,而且具有按精確次序排列的叁數(shù)。
但是因為實例中 RailRoad 和 Utility 類并不需要顏色或者租用數(shù)據(jù), 所以我們需要測試PropertyInfo 也能引入少量的參數(shù)而實例化為RailRoad 和 Utility 類對象:

function testPropertyInfoMissingColorRent() {
$list = array(‘type’,’price’);
$this->assertIsA(
$testprop = new PropertyInfo($list), ‘PropertyInfo’);
$this->assertNoErrors();
foreach($list as $prop) {
$this->assertEqual($prop, $testprop->$prop);
}
$this->assertNull($testprop->color);
$this->assertNull($testprop->rent);
}

注:assertNoErrors()
assertNoErrors() 方法的作用是:證實沒有PHP 錯誤發(fā)生。如果有錯誤, 將不通過測試。
assertNull()
assertNull()方法的作用是:測試第一個參數(shù)是否為空。 如果第一個參數(shù)不為空, 將不通過測試。像大多數(shù)其他測試方法一樣,, 你可以選擇是否使用第二個叁數(shù)定義失敗信息。

為了滿足前面的測試,PropertyInfo 類定義為:

class PropertyInfo {
const TYPE_KEY = 0;
const PRICE_KEY = 1;
const COLOR_KEY = 2;
const RENT_KEY = 3;
public $type;
public $price;
public $color;
public $rent;
public function __construct($props) {
$this->type =
$this->propValue($props, ‘type’, self::TYPE_KEY);
$this->price =
$this->propValue($props, ‘price’, self::PRICE_KEY);
$this->color =
$this->propValue($props, ‘color’, self::COLOR_KEY);
$this->rent =
$this->propValue($props, ‘rent’, self::RENT_KEY);
}
protected function propValue($props, $prop, $key) {
if (array_key_exists($key, $props)) {
return $this->$prop = $props[$key];
}
}
}


現(xiàn)在PropertyInfo 類可以構(gòu)造各種不同的Property參數(shù)了。同時Assessor類可以提供數(shù)據(jù)來建立正確的PropertyInfo對象。
現(xiàn)在以Assessor->$prop_info數(shù)組提供的數(shù)據(jù)為基礎(chǔ),新建一個實例化 PropertyInfo 的類。
這樣的代碼可以是:

class Assessor {
protected $game;
public function setGame($game) { $this->game = $game; }
public function getProperty($name) {
$prop_info = new PropertyInfo($this->prop_info[$name]);
switch($prop_info->type) {
case ‘Street’:
$prop = new Street($this->game, $name, $prop_info->price);
$prop->color = $prop_info->color;
$prop->setRent($prop_info->rent);
return $prop;
case ‘RailRoad’:
return new RailRoad($this->game, $name, $prop_info->price);
break;
case ‘Utility’:
return new Utility($this->game, $name, $prop_info->price);
break;
default: //should not be able to get here
}
}
protected $prop_info = array(/* ... */);
}

這段代碼實現(xiàn)了上述功能, 但卻非常脆弱。如果代入的值是$this->prop_info數(shù)組中沒有的值,結(jié)果會怎樣呢?因為 PropertyInfo 已經(jīng)被實例化并被加入到Assessor代碼中, 沒有有效的方法測試被產(chǎn)生的對象。比較好的解決就是:產(chǎn)生一個工廠方法使 PropertyInfo 對象更容易建立。 因此, 下一步將是寫一個測試來實現(xiàn)Assessor類中的PropertyInfo方法。

但是,有一個問題: 這個方法不應該是Assessor類的公共接口(API)的一個部份。它能被測試嗎?

這里有兩個方法, 可以探究任何要求的合理數(shù)量的測試。簡單的說, 你可以運行黑匣子測試或白匣子測試。

注:黑匣子測試(Black Box Testing)
黑匣子測試就是:把被測試的對象當成" 黑匣子 " ,我們只知道它提供的應用接口(API),但不知道其到底執(zhí)行了什么。它主要測試對象公共方法的輸入和輸出。
白匣子測試(White Box Testing)
白匣子測試和黑匣子測試恰恰相反, 它假定知道測試對象中的所有代碼信息。這種形式的測試是為了完善代碼和減少錯誤。
關(guān)于白匣子測試的詳細說明請見:http:// c 2.com/cgi/wiki?WhiteBoxTesting 。

分享:《PHP設(shè)計模式介紹》第一章 編程慣用法
學習一門新的語言意味著要采用新的慣用法。這章將介紹或者可能重新強調(diào)一些慣用法。你會發(fā)現(xiàn)這些慣用法在你要在代碼中實現(xiàn)設(shè)計模式時候是非常有用的。 在這里總結(jié)的許多編程慣用法都是很值得

來源:模板無憂//所屬分類:PHP教程/更新時間:2008-08-22
相關(guān)PHP教程