命令模式

目的

命令模式主要用于封装调用和解耦。

命令模式提供了命令调用者 Invoker 和命令接收者 Receiver 两个角色。该模式使用一条“命令”来委托针对接收者的方法调用,并暴露功能相同的 execute 方法。因此,命令调用者只需调用 execute 方法来处理来自客户端的命令。最终,命令接收者与调用者实现了解耦。

另一方面,命令模式还提供了 undo() 方法,用于撤销之前的 execute() 方法。“命令”也可以聚合起来,即用最少的复制/粘贴以及通过对象组合的方式(而不是继承)来聚合更复杂的命令。

使用场景

  • 文本编辑器:所有的事件都视为“命令”,这些命令可以撤销、追加和保存
  • 大型 CLI 工具使用子命令来分派各种任务,并将它们打包成“模块”,每个模块都可以用命令模式来实现(如:vagrant)

UML 类图

命令模式类图

代码

Command.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command;

interface Command
{
/**
* this is the most important method in the Command pattern,
* The Receiver goes in the constructor.
*/
public function execute();
}

UndoableCommand.php

1
2
3
4
5
6
7
8
9
10
11
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command;

interface UndoableCommand extends Command
{
/**
* This method is used to undo change made by command execution
*/
public function undo();
}

HelloCommand.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command;

/**
* This concrete command calls "print" on the Receiver, but an external
* invoker just knows that it can call "execute"
*/
class HelloCommand implements Command
{
private Receiver $output;

/**
* Each concrete command is built with different receivers.
* There can be one, many or completely no receivers, but there can be other commands in the parameters
*/
public function __construct(Receiver $console)
{
$this->output = $console;
}

/**
* execute and output "Hello World".
*/
public function execute()
{
// sometimes, there is no receiver and this is the command which does all the work
$this->output->write('Hello World');
}
}

AddMessageDateCommand.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command;

/**
* This concrete command tweaks receiver to add current date to messages
* invoker just knows that it can call "execute"
*/
class AddMessageDateCommand implements UndoableCommand
{
private Receiver $output;

/**
* Each concrete command is built with different receivers.
* There can be one, many or completely no receivers, but there can be other commands in the parameters.
*/
public function __construct(Receiver $console)
{
$this->output = $console;
}

/**
* Execute and make receiver to enable displaying messages date.
*/
public function execute()
{
// sometimes, there is no receiver and this is the command which
// does all the work
$this->output->enableDate();
}

/**
* Undo the command and make receiver to disable displaying messages date.
*/
public function undo()
{
// sometimes, there is no receiver and this is the command which
// does all the work
$this->output->disableDate();
}
}

Receiver.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command;

/**
* Receiver is a specific service with its own contract and can be only concrete.
*/
class Receiver
{
private bool $enableDate = false;

/**
* @var string[]
*/
private array $output = [];

public function write(string $str)
{
if ($this->enableDate) {
$str .= ' ['.date('Y-m-d').']';
}

$this->output[] = $str;
}

public function getOutput(): string
{
return join("\n", $this->output);
}

/**
* Enable receiver to display message date
*/
public function enableDate()
{
$this->enableDate = true;
}

/**
* Disable receiver to display message date
*/
public function disableDate()
{
$this->enableDate = false;
}
}

Invoker.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command;

/**
* Invoker is using the command given to it.
* Example : an Application in SF2.
*/
class Invoker
{
private Command $command;

/**
* in the invoker we find this kind of method for subscribing the command
* There can be also a stack, a list, a fixed set ...
*/
public function setCommand(Command $cmd)
{
$this->command = $cmd;
}

/**
* executes the command; the invoker is the same whatever is the command
*/
public function run()
{
$this->command->execute();
}
}

测试

Tests/CommandTest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command\Tests;

use DesignPatterns\Behavioral\Command\HelloCommand;
use DesignPatterns\Behavioral\Command\Invoker;
use DesignPatterns\Behavioral\Command\Receiver;
use PHPUnit\Framework\TestCase;

class CommandTest extends TestCase
{
public function testInvocation()
{
$invoker = new Invoker();
$receiver = new Receiver();

$invoker->setCommand(new HelloCommand($receiver));
$invoker->run();
$this->assertSame('Hello World', $receiver->getOutput());
}
}

Tests/UndoableCommandTest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php declare(strict_types = 1);

namespace DesignPatterns\Behavioral\Command\Tests;

use DesignPatterns\Behavioral\Command\AddMessageDateCommand;
use DesignPatterns\Behavioral\Command\HelloCommand;
use DesignPatterns\Behavioral\Command\Invoker;
use DesignPatterns\Behavioral\Command\Receiver;
use PHPUnit\Framework\TestCase;

class UndoableCommandTest extends TestCase
{
public function testInvocation()
{
$invoker = new Invoker();
$receiver = new Receiver();

$invoker->setCommand(new HelloCommand($receiver));
$invoker->run();
$this->assertSame('Hello World', $receiver->getOutput());

$messageDateCommand = new AddMessageDateCommand($receiver);
$messageDateCommand->execute();

$invoker->run();
$this->assertSame("Hello World\nHello World [".date('Y-m-d').']', $receiver->getOutput());

$messageDateCommand->undo();

$invoker->run();
$this->assertSame("Hello World\nHello World [".date('Y-m-d')."]\nHello World", $receiver->getOutput());
}
}