What does php command mode mean?

(*-*)浩
Release: 2023-02-24 16:38:01
Original
1824 people have browsed it

Command pattern (Command) is a very commonly used design pattern. Its essence is to encapsulate commands, separate the responsibility for issuing commands and the responsibility for executing commands, and reduce the coupling between objects. New The command can be easily added to the system.

What does php command mode mean?

The commonly used advantage of the command mode is that it can completely decouple the request sender and receiver, and the system Decoupling is the ultimate goal of the system.

After decoupling, certain methods of certain objects are called without knowing who the recipient of the request is. (Recommended learning: PHP programming from entry to proficiency)

Artificial intelligence is developing rapidly in the Internet of Things environment. Nowadays, smart lights can be easily controlled by just connecting to a mobile phone, lying in bed in winter Easily implement opening and closing.

Code:

<?php
 
header("Content-Type:text/html;charset=utf-8");
 
require_once "Command.php";
 
// 智能灯
$lamp = new Lamp(); 
 
// 开灯命令
$on = new onCommand($lamp);
// 关灯命令
$off = new offCommand($lamp);
 
// 控制器类  请求者 不用知道对象是谁
$_c = new Control($on,$off);
 
$_c->turnOn();  // 开灯方法
$_c->turnOff(); // 关灯方法
Copy after login

Code:

<?php
// 智能灯类
Class Lamp
{
	/**
     * 开灯方法
     * @access public
     */
    public function TurnOn()
    {
        echo "已帮您打开智能灯<br/>";
    }
 
	/**
     * 关灯方法
     * @access public
     */
    public function TurnOff()
    {
    	echo "已帮您关闭智能灯<br/>";
    }
}
 
// 命令抽象总类
abstract Class ICommand
{
 
	/**
     * 抽象执行方法
     * @access public
     */
	abstract public function Execute();
}
 
// 开灯命令
Class onCommand extends ICommand
{
	/**
     * 智能灯对象
     * @var object private
     */
	private $_lamp;
 
	/**
     * 构造函数
     * @access public
     * @param  object  $lamp 智能灯对象
     */
	function __construct($lamp)
	{
		$this->_lamp = $lamp;
	}
 
	/**
     * 开灯执行方法
     * @access public
     */
    function Execute()
    {
         $this->_lamp->TurnOn();
    }
}
 
// 关灯命令
Class offCommand extends ICommand
{
	/**
     * 智能灯对象
     * @var object private
     */
	private $_lamp;
 
	/**
     * 构造函数
     * @access public
     * @param  object  $lamp 智能灯对象
     */
	function __construct($lamp)
	{
        $this->_lamp = $lamp;
	} 
 
	/**
     * 关灯执行方法
     * @access public
     */
    function Execute()
    {
    	$this->_lamp->TurnOff();
    }	
}
 
// 遥控器类
Class Control
{
	/**
     * 开灯命令对象
     * @var object private
     */
	private $_on;
 
	/**
     * 关灯命令对象
     * @var object private
     */
	private $_off;
 
	/**
     * 构造函数
     * @access public
     * @param  object  $on 开灯对象
     * @param  object  $off 关灯对象
     */	
	function __construct($on,$off)
	{
        $this->_on = $on;
        $this->_off = $off;
	}
 
	/**
     * 开灯函数
     * @access public
     */
	function turnOn()
	{
        $this->_on->Execute();
	}
 
	/**
     * 关灯函数
     * @access public
     */
	function turnOff()
	{
		$this->_off->Execute();
	}
}
Copy after login

Output result:

已帮您打开智能灯
已帮您关闭智能灯
Copy after login

The above is the detailed content of What does php command mode mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!