时间:2021-07-01 10:21:17 帮助过:4人阅读
代码如下:
//命令接口
interface Command{
public function execute();
}
//具体命令
class ConcreteCommand implements Command{
private $_receiver;
public function __construct($receiver){
$this->_receiver = $receiver;
}
public function execute(){
$this->_receiver->action();
}
}
//接受者
class Receiver{
private $_name;
public function __construct($name){
$this->_name = $name;
}
//行动方法
public function action(){
echo $this->_name.'do action .
';
}
}
//请求者
class Invoker{
private $_command;
public function __construct($command){
$this->_command = $command;
}
public function action(){
$this->_command->execute();
}
}
//客户端
class Client{
public static function main(){
$receiver = new Receiver('jaky');
$command = new ConcreteeCommand($receiver);
$invoker = new Invoker($command);
$invoker->action();
}
}
Client::main();
?>
http://www.bkjia.com/PHPjc/736825.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736825.htmlTechArticle命令类: 1.命令角色:声明了一个给所有具体命令类的抽象接口。这是一个抽象角色。 2.具体命令角色:定义一个接受者和行为之间的弱耦...