PHP command mode implementation simple code example

黄舟
Release: 2023-03-06 16:44:01
Original
1341 people have browsed it

PHPCommand modeimplementation of simple code examples

<?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 &#39;Action has been taken!<br/>&#39;;
	}
}

/**
 * 请求者, 命令的请求者
 */
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();
Copy after login

The above is the detailed content of PHP command mode implementation simple code example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template