行为型设计模式 - 命令 Command
作者:菜皮日记
- 2023-09-08 北京
本文字数:1683 字
阅读完需:约 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.)
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
菜皮日记
关注
全干程序员 2018-08-08 加入
还未添加个人简介
评论