In addition to the design principles in Java, there are also 23 design patterns.
These patterns have been accumulated by the predecessors bit by bit, have been improving, and have been optimizing, and these design patterns can solve some specific problems.
And in these modes, it can be said that the use of language is fully reflected.
Then we are going to learn the chain of responsibility model in the behavioral model today !
Chain of Responsibility Model
First come to Baidu Encyclopedia what is the chain of responsibility model
Through Baidu Encyclopedia, we can know that the chain of responsibility model is that the client initiates a request, or call, and then the object on the corresponding chain starts to process the request, and all of this is dynamically organized and allocated without affecting the client.
Definition and characteristics of the chain of responsibility model
The definition of the Chain of Responsibility model: In order to avoid the request sender being coupled with multiple request handlers, all request handlers are connected into a chain by remembering the reference of the next object through the previous object; When a request occurs, the request can be passed along this chain until an object handles it.
(PS: Chain of Responsibility = Chain of Responsibility)
Advantages of the chain of responsibility model:
1. Enhance the scalability of the system.
2. Reduce the degree of coupling between objects.
3. Sharing of responsibilities.
Disadvantages:
1. It can be guaranteed that every request must be processed.
2. Compared with a longer chain of responsibility, the processing of requests may involve multiple processing objects, and system performance will be affected to a certain extent.
The structure and realization of the responsibility chain model
Let me first talk about the roles in this chain of responsibility model
1. Abstract processor role: define an interface for processing requests, including abstract processing methods and a subsequent connection.
2. The role of the specific processor: to implement the processing method of the abstract processor, to determine whether the request can be processed, and if it can be processed, it will be processed, otherwise the request will be forwarded to its successor.
3. Customer 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.
After knowing this, we can make an example to experience the chain of responsibility model
Requirements: To apply for effectiveness, first apply to the project team leader, then to the director, then the vice president, and finally the finance. It's like we have such a chain, let's realize it
first step:
First define an object of abstract handler
With this, it’s not enough, we still need to define who will be the next approval leader, and the method that the current leader needs to implement
The second step:
Let's create a specific leader, here we need to create four leaders (here I will paste a leader, the others are similar)
Others are similar to this
test:
Let's get a test class to test
This is OK, let's call it
Isn’t it OK, everyone feel it
OK, that's it, everyone take a good look. Practice a lot. If you have any questions, please contact me QQ: 2100363119
Welcome everyone to visit my personal website: lemon1234.com Thank you for leaving a message