Software Design Mode Learning Responsibility Chain Mode

If there are multiple objects in the system that can process the same request, the responsibility chain pattern can be u...
Mode Motivation
Schema Definition
Pattern structure
Pattern instance
Advantages and disadvantages of patterns
Model applicable environment
Pure and Pure Responsibility Chain Model

If there are multiple objects in the system that can process the same request, the responsibility chain pattern can be used to link the objects that process the request to form a chain along which the request can be passed.Processing if the object in the chain can handle the request, otherwise forwarding the request to the next person


Mode Motivation

In many cases, more than one person can handle a request, such as university scholarship approval, students submit an approval form to the counselor first, the counselor signs the approval before handing it to the dean for signature approval, followed by the president for approval, and possibly the president for approval. In this process, the scholarship application form can be viewed as a request object, and approvers at different levels canIn order to process the request, in addition to the instructor, students do not have to interact with other approvers one by one, just wait for the results.In the process of approval, if one of the approvers considers that it does not meet the requirements, the request is suspended, otherwise the request is passed to the next approver and the principal will make the final decision.

In this process, counselors, deans, deans, and principals form a chain along which application forms are transferred, called the responsibility chain.The responsibility chain can be a straight line, a ring or a tree structure, and the common responsibility chain is a straight line.Requests are passed along the chain, and are processed by the handler in the chain. Customers do not need to care about the details of request processing and delivery to decouple the requester from the requester.


Schema Definition

Avoid coupling requesters with receivers so that multiple objects may accept requests, join them into a chain, and pass requests along that chain until an object is processed.Responsibility chain mode is also called responsibility chain mode, it is an object behavior mode.


Pattern structure

  1. Handler (Abstract Processor)

    Defines an interface for processing requests, in which abstract request handling methods are defined because different specific processors handle requests differently.Defines an object of a self-type (abstract handler type) in an abstract handler as a reference to the next family.

  2. ConcreteHandler

    The concrete handler is the abstract handler subclass, which implements the abstract request handling method and handles user requests.Before processing a request, make a decision to see if you have the appropriate processing privileges, and if you can, process it, or forward the request to a successor.

  3. Client (Customer Class)

    Used to make initial requests to objects in a chain, the customer class only cares about the source of the chain, not the processing details and the delivery process of the request.


Pattern instance

A system provides a module for examination and approval of holidays. The director can approve the holidays if the number of days an employee takes is less than 3 days. If the number of days an employee takes is more than or equal to 3 days and less than 10 days, the manager can approve the holidays. If the number of days an employee takes is more than or equal to 10 days and less than 30 days, the general manager can approve the holidays.Day, the general manager can not approve, prompt the corresponding rejection information.

  1. Request class LeaveRequest

    // Encapsulate information about the request so that it can be processed by the handler public class LeaveRequest { private String leaveName; private int leaveDays; public LeaveRequest(String leaveName, int leaveDays) { this.leaveName = leaveName; this.leaveDays = leaveDays; } public void setLeaveName(String leaveName) { this.leaveName = leaveName; } public void setLeaveDays(int leaveDays) { this.leaveDays = leaveDays; } public String getLeaveName() { return leaveName; } public int getLeaveDays() { return leaveDays; } }
  2. Abstract Processing Class Leader

    public abstract class Leader { protected String name; protected Leader successor; // As a reference to the family public Leader(String name) { this.name = name; } public void setSuccessor(Leader successor) { this.successor = successor; } public abstract void handleRequest(LeaveRequest request); }
  3. Processor Director (Director Class)

    public class Director extends Leader { public Director(String name) { super(name); } @Override public void handleRequest(LeaveRequest request) { if (request.getLeaveDays() < 3) { System.out.println("director" + name + "Approval Employee" + request.getLeaveName() + "The number of days off is" + request.getLeaveDays() + "day"); } else { if (this.successor != null) { this.successor.handleRequest(request); } } } }
  4. Processor Specific Manager (Manager Class)

    public class Manager extends Leader { public Manager(String name) { super(name); } @Override public void handleRequest(LeaveRequest request) { if (request.getLeaveDays() < 10) { System.out.println("manager" + name + "Approval Employee" + request.getLeaveName() + "The number of days off is" + request.getLeaveDays() + "day"); } else { if (this.successor != null) { this.successor.handleRequest(request); } } } }
  5. Processor General Manager (General Manager class)

    public class GeneralManager extends Leader { public GeneralManager(String name) { super(name); } @Override public void handleRequest(LeaveRequest request) { if (request.getLeaveDays() < 30) { System.out.println("General manager" + name + "Approval Employee" + request.getLeaveName() + "The number of days off is" + request.getLeaveDays() + "day"); } else { System.out.println("Want to take time off" + request.getLeaveDays() + "Days?You don't want to do it!"); } } }
  6. Client Test Class

    public class Client { public static void main(String[] args) { Leader director = new Director("Wang Ming"); Leader manager = new Manager("Zhao Qiang"); Leader generalManager = new GeneralManager("Chen Yong"); director.setSuccessor(manager); manager.setSuccessor(generalManager); LeaveRequest lr1 = new LeaveRequest("Zhang San", 2); director.handleRequest(lr1); LeaveRequest lr2 = new LeaveRequest("Li Si", 5); director.handleRequest(lr2); LeaveRequest lr3 = new LeaveRequest("King Five", 15); director.handleRequest(lr3); LeaveRequest lr4 = new LeaveRequest("Zhao Six", 45); director.handleRequest(lr4); } }
  7. Run Results


Advantages and disadvantages of patterns

The advantages of the responsibility chain model are as follows:

  1. Decrease coupling.Objects don't need to know who handled the request, they just need to wait for the result.Neither the receiver nor the sender has clear information about each other, and the objects in the chain need not know the structure of the chain, so the client is responsible for creating the chain.
  2. Simplify the interconnection of objects.Request processing objects need to maintain only one reference to their successors, not all candidate processors
  3. Enhance flexibility in assigning responsibilities to objects.You can add or change a request by dynamically adding or modifying the chain at run time
  4. It is convenient to add a new request processing class.Add a new specific request handler without modifying the source code, just rebuild the chain on the client side, conforming to the open and close principle

The shortcomings of the responsibility chain model are as follows:

  1. There is no guarantee that the request will be received.The request may not be processed until the end, or it may not be properly configured and processed
  2. For longer chains of responsibility, request processing may involve more than one processing object, system performance will be affected, and code tuning will be inconvenient

Model applicable environment

Consider using the responsibility chain model in the following situations:

  1. There are multiple objects that can handle the same request, and which one is handled at run time
  2. Submit a request to one of multiple objects without explicitly specifying the recipient.Requests are passed along the chain, looking for the appropriate handler
  3. Dynamically specify a set of objects to process requests. Clients can dynamically create a chain of responsibilities to process requests. They can also dynamically change the order between processors in the chain.

Pure and Pure Responsibility Chain Model

A purely chained responsibility pattern requires that a handler object either receive a request, assume responsibility, or transfer responsibility to the next person.In an impure responsibility chain model, a request can be part of the responsibility of a processing object, then downloaded, or ultimately not received by any receiving object.In fact, most of the responsibilities we come into contact with are not pure chain models.

16 May 2020, 23:53 | Views: 9387

Add new comment

For adding a comment, please log in
or create account

0 comments