首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 設計模式:命令模式深度解析與實戰

     編輯:PHP 相關知識

模式定義

命令模式 將一個請求封裝為一個對象,從而可以:

  • 用不同請求參數化客戶端
  • 將請求排隊、記錄日誌
  • 支援可撤銷操作

核心角色

角色職責
Command (命令介面)聲明執行操作的介面,通常包含 execute()
ConcreteCommand (具體命令)綁定接收者與動作,實現 execute() 調用接收者方法
Receiver (接收者)執行具體業務邏輯
Invoker (請求者/調用者)持有命令對象,觸發 execute()
Client (客戶端)創建具體命令、設置接收者、裝配調用者

代碼實現

<?php
// 1. 命令介面
interface Command {
    public function execute();
}

// 2. 接收者
class Light {
    public function turnOn()  { echo "Light is ON
"; } public function turnOff() { echo "Light is OFF
"; } } // 3. 具體命令 class LightOnCommand implements Command { private $light; public function __construct(Light $light) { $this->light = $light; } public function execute() { $this->light->turnOn(); } } class LightOffCommand implements Command { private $light; public function __construct(Light $light) { $this->light = $light; } public function execute() { $this->light->turnOff(); } } // 4. 調用者 class RemoteControl { private $command; public function setCommand(Command $cmd) { $this->command = $cmd; } public function pressButton() { $this->command->execute(); } } // 5. 客戶端 $light = new Light(); $remote = new RemoteControl(); $remote->setCommand(new LightOnCommand($light)); $remote->pressButton(); // Light is ON $remote->setCommand(new LightOffCommand($light)); $remote->pressButton(); // Light is OFF ?>

進階應用:撤銷與日誌

為支援 Undo,命令介面可增加 undo() 方法,具體命令記錄執行前狀態。調用者維護命令歷史棧,實現回滾。

interface Command {
    public function execute();
    public function undo();
}

class DimmerLightOnCommand implements Command {
    private $light;
    private $prevLevel;
    public function __construct(Light $light) { $this->light = $light; }
    public function execute() {
        $this->prevLevel = $this->light->getLevel();
        $this->light->dim(75);
    }
    public function undo() {
        $this->light->dim($this->prevLevel);
    }
}

優缺點總結

優點缺點
解耦調用者與接收者類數量增加,結構較複雜
易於新增命令,符合開閉原則過度使用會導致系統過度碎片化
支援宏命令、隊列、日誌、撤銷

適用場景

  • GUI 按鈕、菜單項動作封裝
  • 異步任務隊列(如 Laravel Jobs)
  • 事務性操作需回滾
  • 宏指令錄製/回放

命令模式是行為型模式中極具實用價值的一種,善用它能讓系統更靈活、可擴展。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved