时间:2021-07-01 10:21:17 帮助过:4人阅读
二.示例:
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();版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了(六)面向对象的设计原则之二,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。