Benpian example by dining at a restaurant, you need to go a la carte restaurant, waiter record when ordering, the waiter record to complete the list of chefs, cooks cooking.
/**
* Interface Command 命令角色
*/
interface Command
{
/**
* @return mixed 执行方法
*/
public function execute();
}
/**
* Class ConcreteCommand 具体命令类,执行具体的行为:厨师
*/
class ConcreteCommand implements Command
{
/**
* @var Receiver 用于存放Receiver类
*/
private $_receiver;
public function __construct(Receiver $receiver)
{
$this->_receiver = $receiver;
}
public function execute()
{
// 厨师一顿操作后,终于完成了
$complete = true;
if ($complete == true) {
$card = '食屎啦你'; // 厨师为了你给他带来了今天大汗淋漓的工作,为了表示感谢,送出了一张卡片
$this->_receiver->action($this->_receiver->_name, $card); // 菜做好了,通知服务员上菜
} else {
echo '滚你丫的别BB,我还没想出来怎么做';
}
}
}
/**
* Class Receiver 接收者角色:服务员
*/
class Receiver
{
/**
* @var string 服务员记录了菜名
*/
public $_name;
public function __construct($name)
{
$this->_name = $name;
}
public function action($name = null, $card = null) // 这个方法是厨师那边调用的
{
echo '服务员上菜: ' . $name;
if (!empty($card)) {
echo "<br>$card";
}
}
}
/**
* Class Invoker 请求者角色:点餐的客人,你
*/
class Invoker
{
/**
* @var Command 存放ConcreteCommand类
*/
private $_command;
public function __construct(Command $command)
{
$this->_command = $command;
}
public function action()
{
$this->_command->execute();
}
}
$receiver = new Receiver('满汉全席'); // 你点了满汉全席,服务员(Receiver类)记录了菜名。
$command = new ConcreteCommand($receiver); // 你看着服务员拿着单子进了厨房,空着手出来,厨师拿到了单子。
$invoker = new Invoker($command); // 你确定厨房已有你这个单子了。
$invoker->action(); // 因为你之前还有朋友没到,朋友到了后你跟服务员说可以做菜了。
Scenario:
The person with a command executor completely decoupled.
Visible, you are in full interaction with the waiter, who cooks do not have to pipe Yes.
Internet search a lot of information, I did not find this mode dim, or am I not understanding?
Eight down, I feel it is easy to understand, but to write out the role, really hard. The key is that few people see. . Therefore, the design patterns more stops. .