Model of responsibility chain in design mode
The responsibility chain pattern is an object's behavior pattern. In the chain of responsibility, many objects are linked together by each object's reference to its next generation. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain is ultimately processing the request, which allows the system to reorganize and assign responsibilities dynamically without affecting the client. --Dr. Shanhong's "Java and Patterns"
Example: Simulation filter, the requested data for sensitive words, Chinese symbols, etc. to filter
First edition: processing of request requests via chained implementations
Filter interface
Public Interface Filter { string DoFilter (String msg);}
View Code
Filter Chain
Public class Filterchain { privatenew arraylist<filter>(); Public Filterchain addfilter (Filter f) { this. Filters.add (f); return This ; } Public List<filter> getfilters () { return filters; }}
View Code
Sensitive word Filter
Public class Implements Filter { @Override public string DoFilter (String msg) { = Msg.replace ("Yellow", " Harmony "); return result;} }
View Code
Symbol Filter
Public class Implements Filter { @Override public string DoFilter (String msg) { = msg.replace (', ', ', ') ). replace ('. ', '. '); return result;} }
View Code
Request message Processing
Public classMessageprocessor {PrivateString message; PrivateFilterchain Filterchain; Publicmessageprocessor (String message, Filterchain filterchain) { This. Message =message; This. Filterchain =Filterchain; } Publicstring Process () {string result=message; for(Filter f:filterchain.getfilters ()) {result=f.dofilter (Result); } returnresult; }}
View Code
Test
Public classTest { Public Static voidMain (string[] args) {String request= "Note, do not put the yellow joke."; Filterchain FC=NewFilterchain (); Fc.addfilter (NewSesitivefilter ()). addfilter (NewSymbolfilter ()); Messageprocessor Processor=Newmessageprocessor (Request, FC); String result=processor.process (); SYSTEM.OUT.PRINTLN (result); }}
View Code
OK, Requst chain processing is complete, but still insufficient: how to handle messages when multiple Filterchain are present? In fact, Filterchain is a large filter, so filterchain can implement the filter interface
Second Edition: Modify the Filterchain implementation filter interface
Public classFilterchainImplementsFilter {Privatelist<filter> filters =NewArraylist<filter>(); PublicFilterchain addfilter (Filter f) { This. Filters.add (f); return This; } @Override Publicstring DoFilter (String msg) {string result=msg; for(Filter f:filters) {result=f.dofilter (Result); } returnresult; }}
View Code
Test
Public classTest { Public Static voidMain (string[] args) {String request= "Be careful not to tell a dirty joke."; Filterchain FC=NewFilterchain (); Fc.addfilter (NewSesitivefilter ()); Filterchain FC2=NewFilterchain (); Fc2.addfilter (NewSymbolfilter ()); Fc.addfilter (FC2); String result=Fc.dofilter (Request); SYSTEM.OUT.PRINTLN (result); }}
View Code
OK, the request can now be handled perfectly, but when it is needed, the Filterchain can handle both the information sent by the client and the message returned by the server.
Third Edition: Increase the processing of response
Message Encapsulation
//Request Public classRequest {PrivateString msg; PublicString getmsg () {returnmsg; } Public voidsetmsg (String msg) { This. msg =msg; }}//Response Public classResponse {PrivateString msg; PublicString getmsg () {returnmsg; } Public voidsetmsg (String msg) { This. msg =msg; }}
View Code
New Filter
Public Interface Filter { void doFilter (Request request, Response Response);}
View Code
Sensitive word Filter
Public class Implements Filter { @Override publicvoid doFilter (Request request, Response Response) { request.setmsg (Request.getmsg (). replace ("yellow", "Harmony")); Response.setmsg (Response.getmsg (). Replace ("Harmony", "yellow"));} }
View Code
Symbol Filter
Public class Implements Filter { @Override publicvoid doFilter (Request request, Response Response) { request.setmsg (Request.getmsg (). Replace (', ', ', '). Replace ('. ', '. ')); Response.setmsg (Response.getmsg (). Replace (', ', ', '). Replace ('. ‘, ‘.‘ )); }}
View Code
Filter Chain
Public classFilterchainImplementsFilter {Privatelist<filter> filters =NewArraylist<filter>(); PublicFilterchain addfilter (Filter f) { This. Filters.add (f); return This; } @Override Public voidDoFilter (Request request, Response Response) { for(Filter f:filters) {f.dofilter (request, response); } }}
View Code
Test
Public classTest { Public Static voidMain (string[] args) {Request Request=NewRequest (); Request.setmsg ("Be careful not to tell a dirty joke."); Filterchain FC=NewFilterchain (); Fc.addfilter (NewSesitivefilter ()); Filterchain FC2=NewFilterchain (); Fc2.addfilter (NewSymbolfilter ()); Fc.addfilter (FC2); Response Response=NewResponse (); Response.setmsg ("Harmonious society, good." "); Fc.dofilter (request, response); System.out.println ("Request:" +request.getmsg ()); System.out.println ("Response:" +response.getmsg ()); }}
View Code
OK, finally can handle reqeust and can deal with response, but! as if processing response order reversed
Fourth Edition: Fix the order of response processing
Modify Filter
Public Interface Filter { void doFilter (Request request, Response Response, Filterchain chain);}
View Code
Sensitive word Filter
Public class Implements Filter { @Override publicvoid doFilter (Request request, Response Response, Filterchain chain) { request.setmsg (request.getmsg (). replace ("yellow", "harmony") + "->sesitivefilter"); Chain.dofilter (Request, response, chain); Response.setmsg (Response.getmsg (). Replace ("Harmony", "yellow") + "->sesitivefilter");} }
View Code
Symbol Filter
Public class Implements Filter { @Override publicvoid doFilter (Request request, Response Response, Filterchain chain) { request.setmsg (request.getmsg (). Replace (', ', ', '). Replace ('. ', '. ') + "->symbolfilter"); Chain.dofilter (Request, response, chain); Response.setmsg (Response.getmsg (). Replace (', ', ', '). Replace ('. ‘, ‘.‘) + "->symbolfilter");} }
View Code
Filter Chain
Public classFilterchainImplementsFilter {Privatelist<filter> filters =NewArraylist<filter>(); Private intindex = 0;//record the order of the filter executed PublicFilterchain addfilter (Filter f) { This. Filters.add (f); return This; } @Override Public voidDoFilter (Request request, Response Response, Filterchain chain) {if(Index = = filters.size ())return; Filter F=Filters.get (index); Index++; F.dofilter (Request, response, chain); }}
View Code
Test
Public classTest { Public Static voidMain (string[] args) {Request Request=NewRequest (); Request.setmsg ("Be careful not to tell a dirty joke."); Filterchain FC=NewFilterchain (); Fc.addfilter (NewSesitivefilter ()); Filterchain FC2=NewFilterchain (); Fc2.addfilter (NewSymbolfilter ()); Fc.addfilter (FC2); Response Response=NewResponse (); Response.setmsg ("Harmonious society, good." "); Fc.dofilter (Request, response, FC); System.out.println ("Request:" +request.getmsg ()); System.out.println ("Response:" +response.getmsg ()); }}
View Code
OK, you are done! This is the processing logic of the filter in the web!
Model of responsibility chain in design mode