命令模式
interface Command
{
public function execute();
}
class Light
{
public function on()
{
echo "on";
}
}
class SimpleRemoteControl
{
private $slot = null;
public function __construct()
{
}
public function setCommand($command) {
$this->slot = $command;
}
public function buttonWasPressed()
{
$this->slot->execute();
}
}
class LightOnCommand implements Command
{
private $light = null;
public function __construct($light)
{
$this->light = $light;
}
public function execute()
{
$this->light->on();
// TODO: Implement execute() method.
}
}
本文介绍了一种常用的设计模式——命令模式,并通过具体实例展示了如何在PHP中实现这一模式。通过定义一个命令接口,实现了将请求封装为一个对象,从而使不同的请求、队列或者日志操作可以被参数化。
1802

被折叠的 条评论
为什么被折叠?



