In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand the chain of responsibility pattern of Java design pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. what is the chain of responsibility model
The client issues a request, and the objects on the chain have the opportunity to process the request, and the client does not need to know who the specific object is. In this way, the decoupling between the requester and the receiver is realized, and the dynamic combination of responsibility chain can be realized on the client side. Make programming more flexible.
Definition: multiple objects have the opportunity to process the request, thus avoiding the coupling relationship between the sender and receiver of the request. Join these objects into a chain and pass the request along the chain until an object processes it. The procedure is actually a recursive call.
The main points are as follows:
1. Multiple objects work together to deal with a task.
2. These objects use a chain storage structure to form a chain, and each object knows its next object.
3. An object handles the task. You can add some operations and pass the object to the next task. You can also end the processing of the task and end the task on this object.
4. The client is responsible for assembling the chain structure, but the client does not need to care who handles the task in the end.
Second, the structure of the responsibility chain model.
The roles involved in the chain of responsibility model are as follows:
Abstract processor (Handler) role: defines an interface for handling requests. If necessary, the interface can define a method to set and return a reference to the next home. This role is usually implemented by a Java abstract class or Java interface. The aggregation relationship of the Handler class in the figure above gives the reference of the concrete subclass to the following, and the abstract method handleRequest () regulates the operation of the subclass in handling the request.
Specific handler (ConcreteHandler) role: after receiving the request, the specific processor can choose to process the request or pass the request to the next home. Since the specific handler holds a reference to the next, if necessary, the specific handler can access the next third, the advantages and disadvantages of the responsibility chain model.
Advantages:
The main function of the responsibility chain model is: dynamic combination, decoupling of requester and receiver.
The requester and receiver are loosely coupled: the requestor does not need to know the recipient or how to deal with it. Each responsibility object is only responsible for its own scope of responsibility, and the rest is left to the successor. The components are completely decoupled.
Dynamic combination of responsibilities: the responsibility chain model will disperse the functions into separate responsibility objects, and then dynamically combine to form a chain when in use, so that the responsibility objects can be assigned flexibly, and the responsibility of changing objects can also be added flexibly.
Disadvantages:
Produce a lot of fine-grained objects: because the functional processing is scattered into separate responsibility objects, each object has a single function, in order to deal with the whole process, we need a lot of responsibility objects, which will produce a large number of fine-grained responsibility objects.
May not be able to handle: each responsibility object is only responsible for its own part, so that a request can appear, and even if the entire chain is completed, there is no responsibility object to deal with it. This requires providing default processing and paying attention to the effectiveness of the construction chain.
Fourth, the use scene of the responsibility chain model.
1. If there are multiple objects that can handle the same request, but which object is dynamically determined by the run time, this object can use the responsibility chain pattern to implement the object processing the request into a responsibility object, and then construct the chain. when the request is passed in this chain, it will be judged according to the running status.
two。 Submit a request to one of a number of objects if the request handler is not clear.
3. You need to dynamically specify a collection of objects to process a request
Fifth, the realization of the responsibility chain model.
The Handler class, which defines an interface for handling requests
/ / Manager-- Handler class that defines an interface abstract class Manager {protected string name; / / Manager Superior protected Manager superior; public Manager (string name) {this.name = name;} / / set Manager Superior-key method public void SetSuperior (Manager superior) {this.superior = superior;} abstract public void RequestApplications (Request request);}
Specific processing class, processing the request it is responsible for, can access its successor, if it can handle it, otherwise the request will be transferred to the successor
I. what is the chain of responsibility model
The client issues a request, and the objects on the chain have the opportunity to process the request, and the client does not need to know who the specific object is. In this way, the decoupling between the requester and the receiver is realized, and the dynamic combination of responsibility chain can be realized on the client side. Make programming more flexible.
Definition: multiple objects have the opportunity to process the request, thus avoiding the coupling relationship between the sender and receiver of the request. Join these objects into a chain and pass the request along the chain until an object processes it. The procedure is actually a recursive call.
The main points are as follows:
1. Multiple objects work together to deal with a task.
2. These objects use a chain storage structure to form a chain, and each object knows its next object.
3. An object handles the task. You can add some operations and pass the object to the next task. You can also end the processing of the task and end the task on this object.
4. The client is responsible for assembling the chain structure, but the client does not need to care who handles the task in the end.
Second, the structure of the responsibility chain model.
The roles involved in the chain of responsibility model are as follows:
Abstract processor (Handler) role: defines an interface for handling requests. If necessary, the interface can define a method to set and return a reference to the next home. This role is usually implemented by a Java abstract class or Java interface. The aggregation relationship of the Handler class in the figure above gives the reference of the concrete subclass to the following, and the abstract method handleRequest () regulates the operation of the subclass in handling the request.
Specific handler (ConcreteHandler) role: after receiving the request, the specific processor can choose to process the request or pass the request to the next home. Since the specific handler holds a reference to the next, if necessary, the specific handler can access the next third, the advantages and disadvantages of the responsibility chain model.
Advantages:
The main function of the responsibility chain model is: dynamic combination, decoupling of requester and receiver.
The requester and receiver are loosely coupled: the requestor does not need to know the recipient or how to deal with it. Each responsibility object is only responsible for its own scope of responsibility, and the rest is left to the successor. The components are completely decoupled.
Dynamic combination of responsibilities: the responsibility chain model will disperse the functions into separate responsibility objects, and then dynamically combine to form a chain when in use, so that the responsibility objects can be assigned flexibly, and the responsibility of changing objects can also be added flexibly.
Disadvantages:
Produce a lot of fine-grained objects: because the functional processing is scattered into separate responsibility objects, each object has a single function, in order to deal with the whole process, we need a lot of responsibility objects, which will produce a large number of fine-grained responsibility objects.
May not be able to handle: each responsibility object is only responsible for its own part, so that a request can appear, and even if the entire chain is completed, there is no responsibility object to deal with it. This requires providing default processing and paying attention to the effectiveness of the construction chain.
Fourth, the use scene of the responsibility chain model.
1. If there are multiple objects that can handle the same request, but which object is dynamically determined by the run time, this object can use the responsibility chain pattern to implement the object processing the request into a responsibility object, and then construct the chain. when the request is passed in this chain, it will be judged according to the running status.
two。 Submit a request to one of a number of objects if the request handler is not clear.
3. You need to dynamically specify a collection of objects to process a request
Fifth, the realization of the responsibility chain model.
The Handler class, which defines an interface for handling requests
/ / Manager-- Handler class that defines an interface abstract class Manager {protected string name; / / Manager Superior protected Manager superior; public Manager (string name) {this.name = name;} / / set Manager Superior-key method public void SetSuperior (Manager superior) {this.superior = superior;} abstract public void RequestApplications (Request request);}
Specific processing class, processing the request it is responsible for, can access its successor, if it can handle it, otherwise the request will be transferred to the successor
/ / "Manager class" can inherit this "Manager" class, and only need to rewrite the method of "Application request" / / Manager class class CommonManager: Manager {public CommonManager (string name): base (name) {} public override void RequestApplications (Request request) {/ / the manager has the authority to approve subordinates' two-day leave if (request.RequestType = "leave" & & request.Number
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 226
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.