<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                >命令模式 (Command Pattern): 將一個請求封裝為一個對象,從而使我們可用不同的請求對客戶進行參數化;對請求排隊或者記錄請求日志,以及支持可撤銷的操作。命令模式是一種對象行為型模式,其別名為動作(Action)模式或事務(Transaction)模式。 ## (一)為什么需要命令模式 1, 使用命令模式,能夠讓請求發送者與請求接收者消除彼此之間的耦合,讓對象之間的調用關系更加靈活。 2,使用命令模式可以比較容易地設計一個命令隊列和宏命令(組合命令),而且新的命令可以很容易地加入系統 ## (二)命令模式 UML圖 ![](https://box.kancloud.cn/0cd7b48297dbc547bbd462de543c258b_829x595.png) ## (三)簡單實例 有關命令模式的一個經典的例子,就是電視機與遙控器。如果按照命令模式的UML圖那么,就有這樣的對應關系:電視機是請求的接收者,遙控器是請求的發送者。遙控器上有一些按鈕,不同的按鈕對應電視機的不同操作。這些按鈕就是對應的具體命令類。抽象命令角色由一個命令接口來扮演,有三個具體的命令類實現了抽象命令接口,這三個具體命令類分別代表三種操作:打開電視機、關閉電視機和切換頻道。 <?php //抽象命令角色 abstract class Command{ protected $receiver; function __construct(TV $receiver) { $this->receiver = $receiver; } abstract public function execute(); } //具體命令角色 開機命令 class CommandOn extends Command { public function execute() { $this->receiver->action(); } } //具體命令角色 關機機命令 class CommandOff extends Command { public function execute() { $this->receiver->action(); } } //命令發送者 --遙控器 class Invoker { protected $command; public function setCommand(Command $command) { $this->command = $command; } public function send() { $this->command->execute(); } } //命令接收者 Receiver =》 TV class TV { public function action() { echo "接收到命令,執行成功".PHP_EOL; } } //實例化命令接收者 -------買一個電視機 $receiver = new TV(); //實例化命令發送者-------配一個遙控器 $invoker = new Invoker(); //實例化具體命令角色 -------設置遙控器按鍵匹配電視機 $commandOn = new CommandOn($receiver); $commandOff = new CommandOff($receiver); //設置命令 ----------按下開機按鈕 $invoker->setCommand($commandOn); //發送命令 $invoker->send(); //設置命令 -----------按下關機按鈕 $invoker->setCommand($commandOff); //發送命令 $invoker->send(); 在實際使用中,命令模式的receiver經常是一個抽象類,就是對于不同的命令,它都有對應的具體命令接收者。命令模式的本質是對命令進行封裝,將發出命令的責任和執行命令的責任分割開。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看