Basic concepts
- definition : Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and the recipient of the request . link These objects together and pass the request along this chain, knowing that there are objects to handle it.
- The role division of the COR (chain of responsibility) model :
- Handler: Abstract processor, defining an interface for processing requests
- Concrete Handler: The specific handler, the specific class of processing the request, or to "the other".
- Requester: A class that makes a request to wait for processing, it does not need to pay attention to exactly which specific handler handles its request
- Cor Scenarios for handling problems :
- A request selects the appropriate processing in multiple handler
- traditional through Switch or if-else , and then execute
- responsibilities are unclear and do not meet the single responsibility guidelines
- code bloat, Poor readability
- overload, request invocation Concrete Implementation , inconvenience and handler expansion, loss of interface-oriented programming benefits of low coupling at the implementation level
- does not package for selection logic , specific implementations are exposed, exceptions are easy
- COR ( Responsibility chain) mode, the abstract handler interface is provided
- The implementation of each specific handler is not coupled with requester and only needs to define its own Process the level , and then either take responsibility for a request, make a response , or request forward to subsequent links
- requester do not need to know the specifics of the handler for me , just submit a request
- the advantages and disadvantages of the COR (chain of responsibility) model :
- Advantages :
- The parent class implements the request passing function, the subclass implements the request processing, conforms to the single responsibility principle , each class implements an action or the logic, namely only one reason causes the class change, the subclass implementation is very simple , the responsibility chain establishment is also very flexible the.
- The chain of responsibility mask the process of request processing , you initiate a request who is dealing with, this you do not care, as long as the request to the responsibility chain, you will get results, do not care about the specific who deal with, then the core of the chain of responsibility
- Disadvantages :
- The main problem is performance , when the chain of responsibility is long, performance will fall quickly
- Not easy to debug
Example of COR (chain of responsibility) mode
In peacetime life, small partners work together, often in the face of problems will shake the pot , but eventually always someone to back the pot, of course, is the person responsible for the matter (although there may sometimes be innocent), but the pot is to back, 23333, let's see how this program is implemented with the responsibility chain pattern:
ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;/** * Created by Liulin on 16-4-26. * *Class errorrequester{//Mark the type of request PrivateString duty; PublicStringgetduty() {returnDuty } Public void setduty(String duty) { This. duty = Duty; }}AbstractClass handler{//Record the next element of the current responsibility chain PrivateHandler Nexthandler;//Mark the type of the current subclass PrivateString duty; Public Static FinalString dba="SQL and the Database"; Public Static FinalString front="HTML and CSS"; Public Static FinalString bgd="Java and XML";//Convenient subclasses set their own type (subclasses hide the implementation through a parameterless constructor) Public Handler(String duty) { This. duty = Duty; }The implementation of the responsibility transfer logic of the liability chain, which does not support the coverage of subclasses Public Final void Handlemessage(Errorrequester requester) {if(Requester.getduty (). Equals (Duty)) {Response (requester); }Else{if(Nexthandler! =NULL) {nexthandler.handlemessage (requester); }Else{System.out.println ("The Error is due to others!!!"); } } } Public Final void Setnexthandler(Handler Handler) {Nexthandler = handler; }The function that responds to the request when the//todo matches to the appropriate type protected Abstract void Response(Errorrequester requester);} Class Dbahandler extends handler{//Set the processor type of the current subclass Public Dbahandler() {Super(HANDLER.DBA); }//If the current type matches, then the response @Override protected void Response(Errorrequester requester) {System.out.println ("What horrible"+ requester.getduty () +"is!!!!"); }}class Bgdhandler extends handler{ Public Bgdhandler() {Super(HANDLER.BGD); }@Override protected void Response(Errorrequester requester) {System.out.println ("What horrible"+ requester.getduty () +"is!!!"); }}class Fronthandler extends handler{ Public Fronthandler() {Super(Handler.front); }@Override protected void Response(Errorrequester requester) {System.out.println ("What horrible"+ requester.getduty () +"is!!!"); }} Public class cortest { Public Static void Main(String [] args) {//Construction dictionarieshashmap<integer,string> dic =NewHashMap (); Dic.put (0, HANDLER.DBA); Dic.put (1, HANDLER.BGD); Dic.put (2, Handler.front);intTimes =0;//Establish a chain of responsibilityHandler DBA =NewDbahandler (); Handler BGD =NewBgdhandler (); Handler Front =NewFronthandler (); Dba.setnexthandler (BGD); Bgd.setnexthandler (front);//Generate a random request, and respond while(times++! =Ten) {Errorrequester requester =NewErrorrequester (); Requester.setduty (Dic.get (int) (Math.random () * -)%3) ); Dba.handlemessage (requester); } }}
The results are as follows:
The application of Cor in Tomcat
The container setting for Tomcat is the chain of responsibility , from engine to host to context, knowing that Wraper is passing through a chain of requests.
- The division of roles among them:
- Container: Abstract Handler
- standardengine .... etc. : Specific handler
- A new role is introduced in comparison to the standard responsibility chain:
- PipeLine: As a pipeline for request delivery
- Value: Processing the request on the pipe
- To prevent requests from flowing to the next container , each segment of pipeline always has a node that guarantees it will be able to flow to the next container, so each container has a standardxxxvalue
Java design Pattern (vii) COR (responsibility chain) model and Tomcat extension