one. Introduction:
Command mode: The command mode is divided into "requester of command" and "implementer of command". This completes the decoupling of command requests and implementation.
2. Example:
<?php /* * 模拟服务员与厨师 */ class MealCommand implements Command{ private $cook; public function __construct(cook $cook){ $this->cook = $cook; } public function execute(){ $this->cook->meal(); } } class DrinkCommand implements Command{ private $cook; public function __construct(cook $cook){ $this->cook = $cook; } public function execute(){ $this->cook->drink(); } } /* * 模拟类 */ class cookControl{ private $mealCommand; private $drinkCommand; public function addCommand(Command $mealCommand,Command $drinkCommand){ $this->mealCommand = $mealCommand; $this->drinkCommand = $drinkCommand; } public function callMeal(){ $this->mealCommand->execute(); } public function callDrink(){ $this->drinkCommand->execute(); } } $control = new cookControl(); $cook = new cook; $mealCommand = new MealCommand($cook); $drinkCommand = new DrinkCommand($cook); $control->addCommand($mealCommand,$drinkCommand); $control->callMeal(); $control->callDrink();
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above has introduced the second principle of (6) object-oriented design, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.