-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest.php
More file actions
173 lines (134 loc) · 2.73 KB
/
test.php
File metadata and controls
173 lines (134 loc) · 2.73 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* 命令模式之宏命令 2010-08-22 00:10
* @author phppan.p#gmail.com
* @revision by 0xnz <yunxinyi@gmail.com>
* @update: 2013-09-27
*/
/**
* 命令角色
*/
interface Command {
/**
* 执行方法
*/
public function execute();
}
/**
* 宏命令接口
*/
interface MacroCommand extends Command {
/**
* 宏命令聚集的管理方法,可以删除一个成员命令
* @param Command $command
*/
public function remove(Command $command);
/**
* 宏命令聚集的管理方法,可以增加一个成员命令
* @param Command $command
*/
public function add(Command $command);
}
class DemoMacroCommand implements MacroCommand {
private $_commandList;
public function __construct() {
$this->_commandList = array();
}
public function remove(Command $command) {
$key = array_search($command, $this->_commandList);
if ($key === FALSE) {
return FALSE;
}
unset($this->_commandList[$key]);
return TRUE;
}
public function add(Command $command) {
return array_push($this->_commandList, $command);
}
public function execute() {
foreach ($this->_commandList as $command) {
$command->execute();
}
}
}
/**
* 具体拷贝命令角色
*/
class CopyCommand implements Command {
private $_receiver;
public function __construct(Receiver $receiver) {
$this->_receiver = $receiver;
}
/**
* 执行方法
*/
public function execute() {
$this->_receiver->copy();
}
}
/**
* 具体拷贝命令角色
*/
class PasteCommand implements Command {
private $_receiver;
public function __construct(Receiver $receiver) {
$this->_receiver = $receiver;
}
/**
* 执行方法
*/
public function execute() {
$this->_receiver->paste();
}
}
/**
* 接收者角色
*/
class Receiver {
/* 接收者名称 */
private $_name;
public function __construct($name) {
$this->_name = $name;
}
/**
* 复制方法
*/
public function copy() {
printf("%s do copy action\n", $this->_name);
}
/**
* 粘贴方法
*/
public function paste() {
printf("%s do paste action\n", $this->_name);
}
}
/**
* 请求者角色
*/
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('phpppan');
$pasteCommand = new PasteCommand($receiver);
$copyCommand = new CopyCommand($receiver);
$macroCommand = new DemoMacroCommand();
$macroCommand->add($copyCommand);
$macroCommand->add($pasteCommand);
$invoker = new Invoker($macroCommand);
$invoker->action();
$macroCommand->remove($copyCommand);
$invoker = new Invoker($macroCommand);
$invoker->action();
}
}
Client::main();
?>