1. 将一个请求封装为一个对象,从而是你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作<br />也被称为事务模式<br />举个例子:<br />饭店吃饭,顾客是命令的下达者,服务员是命令的接收者,菜单是整个命令,厨师是命令的执行者<br />这个模式解决了,修改菜单命令的时候,只需要跟服务员说就行了,实现了顾客厨师的解耦,可以实现一个命令接收者,多个命令,(下单,拿酒,上菜),或者把一条命令转达给多个执行者(热菜,凉菜)这个才是命令模式的发挥作用的地方<br />创建执行者,放入命令列表,接收者调用命令 ok了<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22438777/1635477896085-72fa4da1-eebd-4bae-ac21-aee539f1a552.png#clientId=u436079fb-0799-4&from=paste&height=382&id=u4f0fae58&margin=%5Bobject%20Object%5D&name=image.png&originHeight=382&originWidth=817&originalType=binary&ratio=1&size=44009&status=done&style=none&taskId=u4430bdf0-4749-4ac6-aa04-f3acaab3ba6&width=817)
    <?php
    
    //命令的请求类
    class Invoker
    {
        //命令类
        public $command = array();
    
        //添加命令
        public function addVoker($command)
        {
            $this->command[] = $command;
        }
    
        //执行命令
        public function exec()
        {
            //命令类的执行方法
            foreach ($this->command as $value) {
                $value->execute();
            }
        }
    }
    
    //命令抽象类
    abstract class Command
    {
        //执行类
        protected $receiver = array();
    
        //添加执行类
        public function AddCommand($receiver)
        {
            $this->receiver[] = $receiver;
        }
    
        abstract public function execute();
    }
    
    //命令实现类
    class ConcreteCommand extends Command
    {
        public function execute()
        {
            foreach ($this->receiver as $value) {
                $value->action();
            }
        }
    }
    
    //最终的执行类
    class ReceiverA
    {
        //名字
        public $name;
    
        public function __construct($name)
        {
            $this->name = $name;
        }
    
        public function action()
        {
            echo $this->name . '命令执行了!', PHP_EOL;
        }
    }
    
    //最终的执行类
    class ReceiverB
    {
        //名字
        public $name;
    
        public function __construct($name)
        {
            $this->name = $name;
        }
    
        public function action()
        {
            echo $this->name . '命令撤销了!', PHP_EOL;
        }
    }
    
    //先实例化执行类
    $ReceiverA1=new ReceiverA('宫保鸡丁');
    $ReceiverA2=new ReceiverA('京酱肉丝');
    $ReceiverA3=new ReceiverA('水煮肉片');
    $ReceiverB=new ReceiverB('京酱肉丝');
    
    //添加命令
    $ConcreteCommand=new ConcreteCommand();
    $ConcreteCommand->AddCommand($ReceiverA1);
    $ConcreteCommand->AddCommand($ReceiverA2);
    $ConcreteCommand->AddCommand($ReceiverA3);
    
    $ConcreteCommand1=new ConcreteCommand();
    $ConcreteCommand1->AddCommand($ReceiverB);
    
    //请求类
    $Invoker= new Invoker();
    $Invoker->addVoker($ConcreteCommand);
    $Invoker->addVoker($ConcreteCommand1);
    $Invoker->exec();