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通過存儲所有的對象都共享的屬性少通用。雖然如果我想覆蓋某個對象,我可能必須創建方法。