Home > Backend Development > PHP Tutorial > Detailed explanation and cases of php command mode

Detailed explanation and cases of php command mode

墨辰丷
Release: 2023-03-25 22:16:02
Original
1684 people have browsed it

This article mainly introduces the detailed explanation and cases of PHP command mode. Interested friends can refer to it. I hope it will be helpful to everyone.

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. degree, new commands can be easily added to the system.

The commonly used advantage of the command mode is that it can completely decouple the request sender and receiver, and 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.

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. They can be easily turned on and off while lying in bed in winter.

<?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
<?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

Related recommendations:

JS Command Pattern Concept and Usage Analysis

JS Design Pattern Command Pattern Detailed Explanation

JS command pattern concept and usage sharing

The above is the detailed content of Detailed explanation and cases of php command mode. 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