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

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

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

      下面一個(gè)測(cè)試代碼就是測(cè)試 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);
      }
      }

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

      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() 方法的作用是:證實(shí)沒有PHP 錯(cuò)誤發(fā)生。如果有錯(cuò)誤, 將不通過測(cè)試。
      assertNull()
      assertNull()方法的作用是:測(cè)試第一個(gè)參數(shù)是否為空。 如果第一個(gè)參數(shù)不為空, 將不通過測(cè)試。像大多數(shù)其他測(cè)試方法一樣,, 你可以選擇是否使用第二個(gè)叁數(shù)定義失敗信息。

      為了滿足前面的測(cè)試,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ù)了。同時(shí)Assessor類可以提供數(shù)據(jù)來建立正確的PropertyInfo對(duì)象。
      現(xiàn)在以Assessor->$prop_info數(shù)組提供的數(shù)據(jù)為基礎(chǔ),新建一個(gè)實(shí)例化 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(/* ... */);
      }

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

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

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

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

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

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