1. Basic introduction of responsibility chain mode
1) The Chain of Responsibility Pattern, also known as the responsibility Chain pattern, creates a Chain of recipient objects (simple schematic diagram) for requests. This pattern is useful for requests
The sender and receiver are decoupled.
2) The responsibility chain pattern typically includes a reference to another recipient for each recipient. If an object cannot process the request, it passes the same request to the next recipient, and so on.
3) This type of design pattern belongs to behavioral pattern
2. Schematic class diagram of responsibility chain mode
>Description of schematic class diagram - i.e. (roles and responsibilities of responsibility chain model)
1) Handler: an abstract handler, which defines an interface for processing requests and means another handler
2) ConcreteHandler a, B # is a specific handler that handles the request it is responsible for and can access its successor (i.e. the next handler),
If the current request can be processed, it will be processed; otherwise, the request will be handed over to the successor to process, thus forming a responsibility chain
3) Request means many attributes and represents a request
The client issuing the request does not know which object in the chain will eventually process the request, which makes the system dynamically reorganize and allocate responsibilities without affecting the client
3. Application of responsibility chain model
Application scenario:
In the logistics industry, EDI message (XML format file) transmission and receipt reception are usually involved. Each time an EDI message is sent, the associated receipt will be received (identifying the flow status of the data in the third-party system).
Here are several receipt types: MT1101, MT2101, MT4101, MT8104, MT8105 and MT9999. After receiving different receipt messages, the system will execute the corresponding business logic processing. Of course, the actual business scenario is not so general. Here we take receipt processing as a demonstration case
Example of problem code:
public class Receipt { /** * Receipt information */ String message; /** * Receipt type (` MT1101, MT2101, MT4101, MT8104, MT8105, MT9999 ') */ String type; } public class ReceiptBuilder { public static List<Receipt> generateReceiptList(){ //Directly simulate a pile of receipt objects List<Receipt> receiptList = new ArrayList<>(); receiptList.add(new Receipt("I am MT2101 Receipt","MT2101")); receiptList.add(new Receipt("I am MT1101 Receipt","MT1101")); receiptList.add(new Receipt("I am MT8104 Receipt","MT8104")); receiptList.add(new Receipt("I am MT9999 Receipt","MT9999")); //...... return receiptList; } } Traditional practice-if-else branch List<Receipt> receiptList = ReceiptBuilder.generateReceiptList(); //Cyclic processing for (Receipt receipt : receiptList) { if (StringUtils.equals("MT2101",receipt.getType())) { System.out.println("Received MT2101 receipt"); System.out.println("Parse receipt content"); System.out.println("Execute business logic"); } else if (StringUtils.equals("MT1101",receipt.getType())) { System.out.println("Received MT1101 receipt"); System.out.println("Parse receipt content"); System.out.println("Execute business logic"); } else if (StringUtils.equals("MT8104",receipt.getType())) { System.out.println("Received MT8104 receipt"); System.out.println("Parse receipt content"); System.out.println("Execute business logic"); } else if (StringUtils.equals("MT9999",receipt.getType())) { System.out.println("Received MT9999 receipt"); System.out.println("Parse receipt content"); System.out.println("Execute business logic"); System.out.println("Push mail"); } // ...... There may be many else if in the future }
Example of responsibility chain mode code:
Receipt handler interface
/** * @Description: Abstract receipt handler interface * @Auther: wuzhazha */public interface IReceiptHandler { void handleReceipt(Receipt receipt,IReceiptHandleChain handleChain); }
Responsibility chain interface
/** * @Description: Responsibility chain interface * @Auther: wuzhazha */public interface IReceiptHandleChain { void handleReceipt(Receipt receipt); }
Responsibility chain interface implementation class
/** * @Description: Responsibility chain implementation class * @Auther: wuzhazha */public class ReceiptHandleChain implements IReceiptHandleChain { //Record current processor location private int index = 0; //Processor set private static List<IReceiptHandler> receiptHandlerList; static { //Get processor object from container receiptHandlerList = ReceiptHandlerContainer.getReceiptHandlerList(); } @Override public void handleReceipt(Receipt receipt) { if (receiptHandlerList !=null && receiptHandlerList.size() > 0) { if (index != receiptHandlerList.size()) { IReceiptHandler receiptHandler = receiptHandlerList.get(index++); receiptHandler.handleReceipt(receipt,this); } } } }