2010-02-12 60 views
2

Room類有很多我以前用我決定使用命令模式的方法。以前,我調用了很多命令,現在看來我必須在每個方法的roomParser類中創建一個方法。如果我想要調用說,setHotelCode我將不得不創建一個方法roomParser迭代和調用該方法。這是我應該使用命令模式的方式嗎?我應該使用命令模式嗎?好像很多工作

<?php 

interface Parseable { 

    public function parse($arr, $dept); 
} 

class Room implements Parseable { 

    protected $_adults; 
    protected $_kids; 
    protected $_startDate; 
    protected $_endDate; 
    protected $_hotelCode; 
    protected $_sessionNs; 
    protected $_minRate; 
    protected $_maxRate; 
    protected $_groupCode; 
    protected $_rateCode; 
    protected $_promoCode; 
    protected $_confCode; 
    protected $_currency = 'USD'; 
    protected $_soapAction; 
    protected $_soapHeaders; 
    protected $_soapServer; 
    protected $_responseXml; 
    protected $_requestXml; 

    public function __construct($startdate,$enddate,$rooms=1,$adults=2,$kids=0) { 
     $this->setNamespace(SESSION_NAME); 
     $this->verifyDates($startdate, $enddate); 

     $this->_rooms= $rooms; 
     $this->_adults= $adults; 
     $this->_kids= $kids; 

     $this->setSoapAction(); 
     $this->setRates(); 
    } 

    public function parse($arr, $dept) { 
     $this->_price = $arr * $dept * rand(); 
     return $this; 
    } 

    public function setNamespace($namespace) { 
     $this->_sessionNs = $namespace; 
    } 

    private function verifyDates($startdate, $enddate) {} 

    public function setSoapAction($str= 'CheckAvailability') { 
     $this->_soapAction = $str; 
    } 

    public function setRates($rates='') { } 

    public function setHotelCode($code) { $this->_hotelCode = $code; } 

    private function getSoapHeader() { 
     return '<?xml version="1.0" encoding="utf-8"?> 
      <soap:Header> 
      </soap:Header>'; 
    } 

    private function getSoapFooter() { 
     return '</soap:Envelope>'; 
    } 

    private function getSource() { 
     return '<POS> 
      <Source><RequestorId ID="" ID_Context="" /></Source> 
      </POS>'; 
    } 

    function requestXml() { 
     $this->_requestXml = $this->getSoapHeader(); 
     $this->_requestXml .='<soap:Body></soap:Body>'; 
     return $this->_requestXml; 
    } 

    private function setSoapHeaders ($contentLength) { 
     $this->_soapHeaders = array('POST /url HTTP/1.1', 
      'Host: '.SOAP_HOST, 
      'Content-Type: text/xml; charset=utf-8', 
      'Content-Length: '.$contentLength); 
    } 
} 

class RoomParser extends SplObjectStorage { 

    public function attach(Parseable $obj) { 
     parent::attach($obj); 
    } 

    public function parseRooms($arr, $dept) { 
     for ($this->rewind(); $this->valid(); $this->next()) { 
      $ret = $this->current()->parse($arr, $dept); 
      echo $ret->getPrice(), PHP_EOL; 
     } 
    } 
} 

$arrive = '12/28/2010'; 
$depart = '01/02/2011'; 
$rooms = new RoomParser($arrive, $depart); 
$rooms->attach(new Room('12/28/2010', '01/02/2011')); 
$rooms->attach(new Room('12/29/2010', '01/04/2011')); 
echo $rooms->count(), ' Rooms', PHP_EOL; 

編輯:我想它可能是更容易,如果我做了RoomParser通過存儲所有的對象都共享的屬性少通用。雖然如果我想覆蓋某個對象,我可能必須創建方法。

回答

1

這個類看起來好像至少在做三件不同的事情? (存儲房間預訂數據,創建SOAP數據包,處理SOAP傳輸)。你的parse()函數有點神祕......

如果你創建了一個更簡單的Room類,該怎麼辦?

class Room 
{ 
    protected $_properties = array('numberOfAdults' => 0, 'numberOfKids' => 0, ... etc); 

    function __set($name, $value) 
    { 
    if (!isset($this->_properties[$name])) throw new Exception('Invalid property ' . $name); 
    $this->_properties[$name] = $value; 
    } 

    ... etc ... 
} 
相關問題