-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcommand.php
68 lines (57 loc) · 1.16 KB
/
command.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
60
61
62
63
64
65
66
67
68
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/8/8
* Time: 00:06
* 命令行模式
* 将请求封装成对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
*/
/**
* 命令行接口
* Interface Command
*/
interface Command {
public function execute();
}
/**
* 接收调用请求的类
* Class Receiver
*/
class Receiver {
public function write($str) {
echo $str;
}
}
/**
* Class HelloCommand
*/
class HelloCommand implements Command {
protected $output;
public function __construct(Receiver $console) {
$this->output = $console;
}
public function execute() {
$this->output->write('hello world');
}
}
/**
* 调用者
* Class Invoker
*/
class Invoker {
protected $command;
public function setCommand(Command $cmd) {
$this->command = $cmd;
}
/**
* @return mixed
*/
public function run() {
$this->command->execute();
}
}
$invokeObj = new Invoker();
$receiverObj = new Receiver();
$invokeObj->setCommand(new HelloCommand($receiverObj));
$invokeObj->run();