-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcommand.php
More file actions
61 lines (48 loc) · 967 Bytes
/
command.php
File metadata and controls
61 lines (48 loc) · 967 Bytes
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* command pattern
*/
interface Command {
public function execute();
}
class ConcreteCommand implements Command {
private $_receiver;
public function __construct(Receiver $receiver) {
$this->_receiver = $receiver;
}
public function execute() {
$this->_receiver->action();
}
}
class Receiver {
/* 接收者名称 */
private $_name;
public function __construct($name) {
$this->_name = $name;
}
public function action() {
printf("%s -> %s\n", $this->_name, __FUNCTION__);
}
}
class Invoker {
private $_command;
public function __construct(Command $command) {
$this->_command = $command;
}
public function action() {
$this->_command->execute();
}
}
class Client {
public static function main() {
$receiver = new Receiver("John");
$command = new ConcreteCommand($receiver);
$invoker = new Invoker($command);
$invoker->action();
$receiver = null;
$command = null;
$invoker = null;
}
}
Client::main();
?>