首页 > 学院 > 开发设计 > 正文

命令模式的简单实现

2019-11-08 00:43:48
字体:
来源:转载
供稿:网友
<?php// 命令模式interface Command{	public function execute();}/** * concrete command, 具体的命令 */class ConcreteCommand implements Command{	PRivate $receiver;	public function __construct(Receiver $r) {		$this->receiver = $r;	}	public function execute() {		$this->receiver->doAction();	}}/** * 接收者, 命令的执行者 */class Receiver{	public function doAction() {		echo 'Action has been taken!<br/>';	}}/** * 请求者, 命令的请求者 */class Invoker{	private $cmd;	public function __construct(Command $cmd) {		$this->cmd = $cmd;	}	/**	 * call command execute	 */	public function action() {		$this->cmd->execute();	}}// test code$r = new Receiver();$cmd = new ConcreteCommand($r);$invoker = new Invoker($cmd);$invoker->action();
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表