关于后端:行为型设计模式命令-Command

51次阅读

共计 2126 个字符,预计需要花费 6 分钟才能阅读完成。

简介

client 收回的申请,都被封装成 Command 对象,每个 Command 对象代表一种操作,Command 具体操作的是 Receiver 对象内的办法。实现动作与指标隔离和复用的目标。

角色

  • 形象 Command
  • Receiver 真正干活的
  • 具体 Command
  • Invoker 保护 commands 队列

类图

图中所示,client 调用 Invoker,Invoker 中保护了 commands 队列,每个 command 实际操作的是 TextEditor 这个真正的 Receiver。

代码

interface Command
{public function execute(): void;
}

class SimpleCommand implements Command
{
    private $payload;

    public function __construct(string $payload)
    {$this->payload = $payload;}

    public function execute(): void
    {echo "SimpleCommand: See, I can do simple things like printing (" . $this->payload . ")\n";
    }
}

class ComplexCommand implements Command
{
    private $receiver;

    private $a;

    private $b;

    public function __construct(Receiver $receiver, string $a, string $b)
    {
        $this->receiver = $receiver;
        $this->a = $a;
        $this->b = $b;
    }

    public function execute(): void
    {
        echo "ComplexCommand: Complex stuff should be done by a receiver object.\n";
        $this->receiver->doSomething($this->a);
        $this->receiver->doSomethingElse($this->b);
    }
}

class Receiver
{public function doSomething(string $a): void
    {echo "Receiver: Working on (" . $a . ".)\n";
    }

    public function doSomethingElse(string $b): void
    {echo "Receiver: Also working on (" . $b . ".)\n";
    }
}

class Invoker
{
    private $onStart;

    private $onFinish;

    public function setOnStart(Command $command): void
    {$this->onStart = $command;}

    public function setOnFinish(Command $command): void
    {$this->onFinish = $command;}

    public function doSomethingImportant(): void
    {
        echo "Invoker: Does anybody want something done before I begin?\n";
        if ($this->onStart instanceof Command) {$this->onStart->execute();
        }

        echo "Invoker: ...doing something really important...\n";

        echo "Invoker: Does anybody want something done after I finish?\n";
        if ($this->onFinish instanceof Command) {$this->onFinish->execute();
        }
    }
}

$invoker = new Invoker();
$invoker->setOnStart(new SimpleCommand("Say Hi!"));
$receiver = new Receiver();
$invoker->setOnFinish(new ComplexCommand($receiver, "Send email", "Save report"));

$invoker->doSomethingImportant();

output:

Invoker: Does anybody want something done before I begin?
SimpleCommand: See, I can do simple things like printing (Say Hi!)
Invoker: ...doing something really important...
Invoker: Does anybody want something done after I finish?
ComplexCommand: Complex stuff should be done by a receiver object.
Receiver: Working on (Send email.)
Receiver: Also working on (Save report.)

本文由 mdnice 多平台公布

正文完
 0