複製程式碼程式碼如下:
/**
* 命令模式
*
* 將一個請求封裝為一個物件從而使你可用不同的請求對客戶進行參數化,對請求排除或記錄請求日誌,以及支援可取消的操作
*/
介面指令/***/
介面指令
{
公用函數execute();
}
呼叫者類別
{
private $_command = array();
public function setCommand($command) {
$this->_command[] = $command;
}
公共函數executeCommand()
{
foreach($this->_command as $command)
{
$command ->execute();
}
}
public function removeCommand($command)
{
$key = array_search($command, $this->_command);
if ($key !== false)
{
unset($this->_command[$key]);
}
}
}
類接收者
{
private $_name = null;
public function __construct($name) {
$this->_name = $name;
}
public function action()
{
}
public function action()
{
}
public function action()
{
}
public function action()
{
}
public function action()
{
}
public function action()
{
}
public function action()
{
}
public 函數>echo $this->_name." action
";
}
public function action1()
{
echo $this->_name." action1
";
}
}
類別ConcreteCommand 實作Command
{
private $_receiver;
公用函數__construct($receiver)
{
this-> $receiver;
}
公用函數execute()
{
$this->_receiver->action();
}
}
實作程式Command
{
private $_receiver;
公共函數__construct($receiver)
{
$this->_receiver = $receiver;
}
公共函數execute()
{
$this->_receiver->action1();
}
}
類別ConcreteCommand2 實作Command
{
private $_receiver;
公用函數__construct( $receiver)
{
$this->_receiver = $receiver;
}
公用函數execute()
{
$this->_receiver->action();
}
}$objRecevier = new Receiver("No.1");
$objRecevier1 = new Receiver("No.22 "); $objRecevier2 = new Receiver("No.3"); $objCommand = new ConcreteCommand($objRecevier); $objCommand1 = new ConcreteCommand1($objRecevier); = new ConcreteCommand($objRecevier1); $objCommand3 = new ConcreteCommand1($objRecevier1); $objCommand4 = new ConcreteCommand2($objRecevier2); // 使用Recevier 的兩個方法。 (); $objInvoker->setCommand($objCommand); $objInvoker->setCommand($objCommand1); $objInvoker->executeCommand(); $objInkerker-. objCommand1); $objInvoker->executeCommand(); $objInvoker->setCommand($objCommand2); $objInvoker->setCommand($objsetCommand3); $objInvoker->setCommand($objsetCommand3); objIn$ker>$objIn$ objCommand4); $objInvoker->executeCommand(); 以上就介紹了統帥php設計模式 Command指令模式,包括了統帥方面的內容,希望對PHP教學有興趣的朋友有幫助。