Home  >  Article  >  Backend Development  >  PHP Design Pattern Command Pattern_PHP Tutorial

PHP Design Pattern Command Pattern_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:13:39906browse

Command mode: Encapsulate a request as an object so that you can parameterize the client with different requests; queue or log requests, and support undoable operations. Command class: 1. Command role: declares an abstract interface for all specific command classes. This is an abstract role. 2. Specific command role: define a weak coupling between the acceptor and the behavior; implement the execute method, which is responsible for calling the corresponding operation of the acceptance. The execute() method is usually called the execution method 3. Customer role: Create a specific command object and determine its recipient. 4. Requester role: Responsible for calling the command object to execute the request. The related method is called the action method. 5. Acceptor role: Responsible for the specific implementation and execution of a request. Function: 1. Abstract the action to be performed to parameterize the object. 2. Specify, sequence and execute requests at different times. 3.Support cancellation operation 4.Support modification log ​

<?<span php
</span><span //</span><span 命令接口</span>
<span interface</span><span  Command{
     </span><span public</span> <span function</span><span  execute();
}
</span><span //</span><span 具体命令</span>
<span class</span> ConcreteCommand <span implements</span><span  Command{
     </span><span private</span> <span $_receiver</span><span ;
     </span><span public</span> <span function</span> __construct(<span $receiver</span><span ){
          </span><span $this</span>->_receiver = <span $receiver</span><span ;
     }
     </span><span public</span> <span function</span><span  execute(){
          </span><span $this</span>->_receiver-><span action();
     }
}
 
</span><span //</span><span 接受者</span>
<span class</span><span  Receiver{
     </span><span private</span> <span $_name</span><span ;
     </span><span public</span> <span function</span> __construct(<span $name</span><span ){
          </span><span $this</span>->_name = <span $name</span><span ;
     }
     </span><span //</span><span 行动方法</span>
     <span public</span> <span function</span><span  action(){
          </span><span echo</span> <span $this</span>->_name.'do action .<br/>'<span ;
     }
}
</span><span //</span><span 请求者</span>
<span class</span><span  Invoker{
     </span><span private</span> <span $_command</span><span ;
     </span><span public</span> <span function</span> __construct(<span $command</span><span ){
          </span><span $this</span>->_command = <span $command</span><span ;
     }
     </span><span public</span> <span function</span><span  action(){
          </span><span $this</span>->_command-><span execute();
     }
}
 
</span><span //</span><span 客户端</span>
<span class</span><span   Client{
     </span><span public</span> <span static</span> <span function</span><span  main(){
          </span><span $receiver</span> = <span new</span> Receiver('jaky'<span );
          </span><span $command</span> = <span new</span> ConcreteeCommand(<span $receiver</span><span );
          </span><span $invoker</span> = <span new</span> Invoker(<span $command</span><span );
          </span><span $invoker</span>-><span action();
     }
}
Client</span>::<span main();
</span>?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440346.htmlTechArticleCommand mode: Encapsulate a request as an object, so that you can parameterize the client with different requests; for Request queuing or logging, and support for undoable operations. ...
Statement:
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