Home >Backend Development >PHP Tutorial >PHP chain of responsibility programming model

PHP chain of responsibility programming model

*文
*文Original
2017-12-27 14:32:371451browse

This article mainly introduces the chain of responsibility programming model of PHP, which is also a programming convention widely used by development teams. I hope to be helpful.

Overview
The chain of responsibility pattern is an object behavior pattern. In the chain of responsibility pattern, many objects are connected to form a chain by each object's reference to its descendant. The request is passed up the chain until an object in the chain decides to handle the request. The client that issued the request does not know which object in the chain ultimately handles the request, which allows the system to dynamically reorganize and allocate responsibilities without affecting the client


Definition of chain of responsibility pattern
Allows multiple objects to have the opportunity to process requests, thus avoiding the coupling relationship between the sender and receiver of the request, and connecting these objects into a chain. and passes the request along this chain until an object handles it.

Advantages of the chain of responsibility model
The most significant advantage is to separate requests and processing. The requester does not need to know who handled the request, and the processor does not need to know the entire request. The two are decoupled, improving the flexibility of the system.

Disadvantages of the chain of responsibility model
The first is performance issues. Each request is traversed from the head of the chain to the end of the chain. Especially when the chain is relatively long, performance is a question. Second, debugging is not very convenient, especially when the chain is relatively long and has many links. Due to the similar recursive method, the logic may be more complicated during debugging.


The roles involved in the chain of responsibility model are as follows:

Abstract handler (Handler) role: defines a request processing interface. If necessary, the interface can define a method to set and return a reference to the next interface. This role is usually implemented by a PHP abstract class or interface. The aggregation relationship of the Handler class in the above figure gives the reference of the specific subclass to the next home. The abstract method handleRequest() standardizes the operation of the subclass to handle the request.
Specific handler (ConcreateHandle) role: The specific handler receives the request Finally, you can choose to process the request or pass the request to the next party. Since the specific processor holds a reference to the next home, if necessary, the specific handler can visit the next home


to see a PHP chain of responsibility model programming example:

  <?php 
  /** 
   * 抽象处理者角色 
   * @author wzy 
   * 
   */ 
  abstract class Handle 
  { 

    /** 
     * 持有后继的责任对象 
     * 
     * @var object 
     */ 
    protected $successor; 
   
    /** 
     * 示意处理请求的方法,虽然这个示意方法是没有传入参素的 
     * 但实际是可以传入参数的,根据具体需要来选择是否传递参数 
     */ 
    public abstract function handleRequest (); 
   
    /** 
     * 取值方法 
     * 
     * @return object 
     */ 
    public function getSuccessor () 
    { 
      return $this->successor; 
    } 
   
    /** 
     * 赋值方法,设置后继的责任对象 
     * 
     * @param object $objsuccessor       
     */ 
    public function setSuccessor ($objsuccessor) 
    { 
      $this->successor = $objsuccessor; 
    } 
  } 
   
  /** 
   * 具体处理者角色 
   * 
   * @author wzy 
   *     
   */ 
  class ConcreateHandler extends Handle 
  { 
   
    /** 
     * 判断是否有后继的责任对象 
     * 如果有,就转发请求给后继的责任对象 
     * 如果没有,则处理请求 
     * 
     * @see Handle::handleRequest() 
     */ 
    public function handleRequest () 
    { 
      if ($this->getSuccessor() != null) { 
        echo "放过请求,将请求转发给后继的责任对象!<br>"; 
        $this->getSuccessor()->handleRequest(); 
      } else { 
        echo "处理请求,处理过程省略...<br>"; 
      } 
    } 
  } 
   
  /** 
   * 客户端代码 
   */ 
   
  // 组装责任链 
  $handle1 = new ConcreateHandler(); 
  $handle2 = new ConcreateHandler(); 
  $handle1->setSuccessor($handle2); 
   
  // 提交请求 
  $handle1->handleRequest(); 
  ?>

It can be seen from the code that the client created two processor objects and specified that the next home of the first processor object is the second processor object, but the second processor object has no next home. . The client then passes the request to the first handler object

Related recommendations:

PHP State Mode Programming

PHP Singleton Pattern Implementation

php Factory Method Pattern

The above is the detailed content of PHP chain of responsibility programming model. For more information, please follow other related articles on the PHP Chinese website!

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