首頁 >後端開發 >php教程 >commander in chief php設計模式 Command指令模式

commander in chief php設計模式 Command指令模式

WBOY
WBOY原創
2016-07-29 08:45:491487瀏覽

複製程式碼程式碼如下:


/**
* 命令模式
*
* 將一個請求封裝為一個物件從而使你可用不同的請求對客戶進行參數化,對請求排除或記錄請求日誌,以及支援可取消的操作
*/
介面指令/***/
介面指令
{
公用函數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();

$this->_receiver->action1();

}

}

$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教學有興趣的朋友有幫助。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn