Design pattern 10 chain of responsibility pattern

background

We have such a scenario, if you need to be reimbursed on a business trip. The approvers of your reimbursement form are: project manager, department director, department head, and financial officer. Of course, you do not need to know the name, phone number, office address and other information of each approver. You don't need to go to these approvers one by one for approval.

A common solution is that you initiate an approval request, your project manager submits it to the department director after approval, the department director submits it to the head of the department after approval, and the person in charge of the department submits it to the person in charge of finance for approval. In this way, you only need to initiate a request, and you don't need to worry about the subsequent approval.

What is the chain of responsibility model

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.Chain the receiving objects and pass the request along the chain until an object handles it. , Thus avoiding the coupling relationship between the sender and receiver of the request. Connect these objects into a chain, and pass the request along this chain until there is an object to process it.)

The chain of responsibility model has obvious advantages. It reduces the coupling between the request object and the processing object, the processing object is easy to expand, and the flexibility of assigning processing objects is enhanced.

The chain of responsibility simplifies the connection between objects. Each object only needs to maintain a reference to its successor, and does not need to maintain references to all other processors, which avoids the use of numerous if or if...else statements.

Chain structure

The responsibility chain model mainly consists of 3 elements:

  • Abstract Handler (Handler) role: Define an interface for processing requests, including abstract processing methods and a subsequent connection.

  • Concrete Handler's role: To implement the processing method of the abstract handler, to determine whether the request can be processed, and if it can handle the request, it will be processed, otherwise the request will be forwarded to its successor.

  • Client role: Create a processing chain and submit a request to the specific handler object at the head of the chain. It does not care about the details of the processing and the process of request delivery.

The structure diagram is as follows:

Responsibility chain structure diagram

Code

Handler

Define an abstract execution class, which has an abstract method for processing requests.

ConcreteHandler1

HandlerIn the same way, we can extend this implementation class ConcreteHandler2.

The test code is as follows:

Test Results:

具体处理者2负责处理该请求!

Views on the chain of responsibility model

So when should we use the chain of responsibility model appropriately?

Generally, if multiple objects handle a request, which object needs to be used is determined by the actual call. In this scenario, we can use the caller mode.

If you have developed a management system, have you ever developed a process approval function? You can think about whether you can use the chain of responsibility model in this regard.

And in the order system, can the processing of order status be better using this design pattern?

However, it should be noted that the recursive method is used in the object. If the link is long, it will also have a certain impact on the performance of the system. Therefore, the use of the chain of responsibility mode requires control of the link length. For example, set the maximum number of nodes in Handler. If the number of nodes exceeds this maximum number, the establishment of the link is not allowed.

In the chain of responsibility mode, each implementation class only needs to pay attention to its own business logic. As for what needs to be handled by itself, let the parent class decide, that is to say, the parent class realizes the function of request transmission, and the child The processing of class implementation requests conforms to the single responsibility principle, and each implementation class only completes one action or logic.

The chain of responsibility model can expand nodes well. Here is a scenario, such as developing a reimbursement approval system. In the beginning, you just developed it as long as the project manager reimbursed it. However, the actual situation is that the reimbursement amount is too large, the project manager can no longer be the master, and the department manager is required to reimburse. If we use the responsibility chain model, we can establish a new node of department manager behind the first processor. When actually calling, just select the node of the department manager.

You see, this expansion method solves the problem of changing requirements very well.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109193591