标签:cti blog es2017 cut this nbsp src 影响 ons
将一个请求封装成一个对象,从而可以用不同的请求对象对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作.
使用场景:
1.可设计一个队列
2.较容易将命令计入日志
3.允许接收的一方是决定是否否决请求
4.可以实现对请求的撤销和重做
5.加入新的命令类不影响其他的类.
6.把请求一个操作的对象与执行操作的对象分离开.
UML
示例代码:
abstract class Command { protected $receiver; public function __construct(Receiver $receiver) { $this->receiver = $receiver; } abstract public function execute($params); } class UserCommand extends Command { public function execute($params) { return $this->receiver->command($params); } } interface Receiver { public function command($params); } class User implements Receiver { public function command($parms) { echo ‘写入日志:用户增加成功‘ . $parms; } } class Invoker { protected $command; public function setCommand(Command $command) { $this->command = $command; } public function execute($params) { return $this->command->execute($params); } } $receiver = new User(); $invoker = new Invoker(); $invoker->setCommand(new UserCommand($receiver)); $invoker->execute(‘jack‘);
标签:cti blog es2017 cut this nbsp src 影响 ons
原文地址:http://www.cnblogs.com/itfenqing/p/7791492.html