-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChain.php
59 lines (51 loc) · 1.41 KB
/
Chain.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
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Created by PhpStorm.
* User: haobin
* Date: 2017/10/9
* Time: 16:41
*/
/**
* 命令链模式以松散耦合主题为基础,发送消息、命令和请求,或通过一组处理程序发送任意内容。
* 每个处理程序都会自行判断自己能否处理请求。如果可以,该请求被处理,进程停止。
* 我们可以为系统添加或移除处理程序,而不影响其他处理程序。
*/
interface ICommand {
function onCommand($name, $args);
}
class CommandChain {
private $_commands = [];
public function addCommand($cmd) {
$this->_commands = $cmd;
}
public function runCommand($name, $args) {
foreach($this->_commands as $command) {
if($command->onCommand($name, $args)) {
return;
}
}
}
}
class UserCommand implements ICommand {
public function onCommand($name, $args)
{
if($name != 'addUser')
return false;
echo "user handind! \n";
return true;
}
}
class MailCommond implements ICommand {
public function onCommand($name, $args)
{
if($name != 'addMail')
return false;
echo "mail handind! \n";
return true;
}
}
$chain = new CommandChain();
$chain->addCommand(new UserCommand());
$chain->addCommand(new MailCommond());
$chain->runCommand('addUser', null);
$chain->runCommand('addMail', null);