Recently, Hebei has been reporting information about the COVID-19 outbreak, so you must pay attention to protection, wear masks, gather less and wash your hands frequently. Believe in Hebei, believe in China, we will survive.

An overview of the

Responsibility chain pattern, also known as responsibility chain pattern, is a kind of behavioral design pattern.

The core idea of the chain of responsibility pattern is to decouple the sender and receiver of the request and process the request by creating a “chain” of receiver objects (that is, connecting multiple receivers).

In the chain of responsibility pattern, each recipient usually contains a reference to the other recipient, so that when a request is processed, it is passed on to the next recipient if it cannot handle the request itself, and so on to the end.

Responsibility chain pattern UML class diagram

Class diagram to explain

Handler: Abstract Handler role; Defines interfaces to handle requests and aggregates a Handler object. ConcreteHandlerA/B: ConcreteHandler role; Inherit Handler and implement the relevant interface to handle requests; Its main role is to process the request for which it is responsible, and to pass it on to the next handler if the request does not belong to it. Request: Indicates the Request role. Client: Client test class.Copy the code

The demo case

Case: The function of approval by different leaders according to different days of leave can be realized through the responsibility chain mode.

Verify the request => Request role

public class VerifyRequest {

  // Days of absence
  private int days;

  // Name of the requester
  private String requestorName;

  / /... The get () and set ();
}
Copy the code

Abstract handler

public abstract class Approver {

  // Next handler
  protected Approver approver;

  // review (how to handle requests)
  public abstract void verify(VerifyRequest request);

  public void setApprover(Approver approver) {
    this.approver = approver; }}Copy the code

Concrete handler

// Team leader
public class GroupLeader extends Approver {

  @Override
  public void verify(VerifyRequest request) {
    if (request.getDays() <= 3) {
      System.out.println("Team leader, yes." + request.getRequestorName()
          + "The" + request.getDays() + "Day off application.");
    } else {
      // Out of the scope of the current handler, let the next handler handleapprover.verify(request); }}}// Project manager
public class ProjectManager extends Approver {

  @Override
  public void verify(VerifyRequest request) {
    if (request.getDays() > 3 && request.getDays() <= 10) {
      System.out.println("Project manager, yes." + request.getRequestorName()
          + "The" + request.getDays() + "Day off application.");
    } else {
      // Out of the scope of the current handler, let the next handler handleapprover.verify(request); }}}/ / the boss
public class Boss extends Approver {

  @Override
  public void verify(VerifyRequest request) {
    if (request.getDays() > 10) {
      System.out.println("Yes, boss." + request.getRequestorName()
          + "The" + request.getDays() + "Day off application.");
    } else {
      // Out of the scope of the current handler, let the next handler handleapprover.verify(request); }}}Copy the code

Client test classes

public class Client {

  public static void main(String[] args) {
    // Create the request
    VerifyRequest request = new VerifyRequest();
    request.setDays(10);
    request.setRequestorName("Programming novice.");

    // Create handler chain
    GroupLeader groupLeader = new GroupLeader();
    ProjectManager projectManager = new ProjectManager();
    Boss boss = new Boss();
    groupLeader.setApprover(projectManager);
    projectManager.setApprover(boss);
    boss.setApprover(groupLeader);

    // Conduct an auditgroupLeader.verify(request); }}Copy the code

The test results

conclusion

The chain of responsibility pattern decouples requests from processing to achieve decoupling and improve system flexibility.

2. The client only needs to send the request to the responsibility chain, and does not need to care about the processing details and the jump process of the request.

3. Using the chain of responsibility mode will affect the performance of the program, especially when the chain is long, so the maximum number of nodes needs to be controlled.

4. The chain of responsibility mode adopts a similar recursive way to complete the processing of requests, which will increase the difficulty of debugging.

This is the end of today’s sharing, if you feel that the article written by “rookie” is still good, remember to like, forward and pay attention to yo! Your support is what keeps me going. Article where to write problems also hope that you can point out, I will be modestly taught.