Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我应该使用命令模式吗?好像有很多工作要做_Php_Design Patterns_Oop - Fatal编程技术网

Php 我应该使用命令模式吗?好像有很多工作要做

Php 我应该使用命令模式吗?好像有很多工作要做,php,design-patterns,oop,Php,Design Patterns,Oop,在我决定使用命令模式之前,我的Room类有很多方法。以前,我调用了很多命令,现在似乎我必须在roomParser类中为每个方法创建一个方法。如果我想调用setHotelCode,我必须在roomParser中创建一个方法,该方法迭代并调用该方法。这是我应该使用的命令模式吗 <?php interface Parseable { public function parse( $arr, $dept ); } class Room implements Parseable {

在我决定使用命令模式之前,我的
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;
setNamespace(会话名称);
$this->verifyDates($startdate,$enddate);
$this->\u rooms=$rooms;
$this->_成人=$maindants;
$this->_kids=$kids;
$this->setSoapAction();
$this->setRates();
}
公共函数解析($arr,$dept){
$this->_price=$arr*$dept*rand();
退还$this;
}
公共函数setNamespace($namespace){
$this->\u sessionNs=$namespace;
}
私有函数verifyDates($startdate,$enddate){}
公共函数setSoPacation($str='CheckAvailability'){
$this->_soapAction=$str;
}
公共函数设置率($rates=''){}
公共函数setHotelCode($code){$this->\u hotelCode=$code;}
私有函数getSoapHeader(){
返回'
';
}
私有函数getSoapFooter(){
返回“”;
}
私有函数getSource(){
返回'
';
}
函数requestXml(){
$this->_requestXml=$this->getSoapHeader();
$this->_requestXml.='';
返回$this->\u requestXml;
}
专用函数setSoapHeaders($contentLength){
$this->_soapHeaders=array('POST/url HTTP/1.1',
“主机:”。SOAP_主机,
'内容类型:text/xml;字符集=utf-8',
“内容长度:”。$contentLength);
}
}
类RoomParser扩展了SplObjectStorage{
公共函数附加(可解析$obj){
父::附加($obj);
}
公共活动室($arr,$dept){
对于($this->rewind();$this->valid();$this->next()){
$ret=$this->current()->parse($arr,$dept);
echo$ret->getPrice(),PHP\u EOL;
}
}
}
$Arrival='12/28/2010';
$departe='01/02/2011';
$rooms=新的RoomParser($arrival,$depart);
$rooms->附加(新房间('12/28/2010'、'01/02/2011'));
$rooms->附加(新房间('12/29/2010'、'01/04/2011'));
echo$rooms->count(),'rooms',PHP_EOL;

编辑:我想如果我通过存储所有对象共享的属性来减少RoomParser的通用性,可能会更容易。不过,如果我想覆盖某个对象,我可能必须创建方法。

这个类看起来至少在做三件不同的事情?(存储房间预订数据、创建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 ...
}