Java design pattern-responsibility chain model

Source: Internet
Author: User

Blog Expert BenefitsC Currency Exchange platform on-lineOctober Recommended Articles Summarydescription of the cyber attack on November 28 csdnJava Design Pattern--responsibility chainCategory: Java Design patterns-java 2012-03-23 15:19 4045 people reading reviews (4) favorite reports Design mode javafilterstringclassstruts

The goal of the responsibility chain model (Chain of Responsibility) is to allow multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Connect these objects as a chain and pass the request along this chain until an object is processed.

You may want to add different processing logic to the request when you are dealing with a user's request, and you can use the responsibility chain to design it at this time. It is convenient to add a processing node when you need to add a processing logic.

Now our requirement is to process the user's request, to layer the user's submitted string information, and to return the result when the processing is complete, and to process the returned string in the same order that it was processed when the returned case was processed in the reverse order.

First set up the user request and receive object requests and response:

[Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. Public class Request {
  3. String Requeststr;
  4. Public String Getrequeststr () {
  5. return requeststr;
  6. }
  7. public void Setrequeststr (String requeststr) {
  8. this.requeststr = requeststr;
  9. }
  10. }
Package Com.lcq.filter;public class Request {string Requeststr;public string Getrequeststr () {return requeststr;} public void Setrequeststr (String requeststr) {this.requeststr = Requeststr;}}

[Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. Public class Response {
  3. String Responsestr;
  4. Public String Getresponsestr () {
  5. return responsestr;
  6. }
  7. public void Setresponsestr (String responsestr) {
  8. this.responsestr = responsestr;
  9. }
  10. }
Package Com.lcq.filter;public class Response {string Responsestr;public string Getresponsestr () {return responsestr;} public void Setresponsestr (String responsestr) {this.responsestr = Responsestr;}}

We will process the logical abstraction of user information into one filter, further abstracting out the filter interface filter:
[Java]View Plaincopyprint?
    1. Package com.lcq.filter;
    2. Public interface Filter {
    3. public void DoFilter (Request request, Response Response,filterchain chain);
    4. }
Package Com.lcq.filter;public interface Filter {public void DoFilter (Request request, Response Response,filterchain Chain);}

Note that there is a filterchain variable in the Dofilter method parameter in the Filte interface, and we then build the Filterchain class: [Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Public class Filterchain implements Filter {
  5. list<filter> filters = new arraylist<filter> ();
  6. int index = 0;
  7. Public Filterchain addfilter (Filter f) {
  8. This.filters.add (f);
  9. return this ;
  10. }
  11. @Override
  12. public void DoFilter (Request request, Response Response, Filterchain chain) {
  13. if (index = = filters.size ())
  14. return;
  15. Filter f = filters.get (index);
  16. index++;
  17. F.dofilter (Request, response, chain);
  18. }
  19. }
Package Com.lcq.filter;import Java.util.arraylist;import Java.util.list;public class Filterchain implements filter { list<filter> filters = new arraylist<filter> (); int index = 0;public filterchain addfilter (Filter f) {This.fil Ters.add (f); return this;} @Overridepublic void DoFilter (Request request, Response Response, Filterchain chain) {if (index = = filters.size ()) return; Filter f = filters.get (index); Index++;f.dofilter (request, response, chain);}}

The filter interface is inherited in Filterchain, which implements the Dofilter method, and in Filterchain there is an index variable that is used to mark which filter is currently accessed, which is stored in the ArrayList, In this way, users can implement their own filters when they are in use, write their own processing logic, add their own filters to ArrayList, and then call Filterchain's Dofilter method to traverse the entire chain of responsibility.

Below we write three filters:

Htmlfilter class:

[Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. /**
  3. * Filter script elements in HTML
  4. * @author LCQ
  5. *
  6. */
  7. Public class Htmlfilter implements Filter {
  8. @Override
  9. public void DoFilter (Request request, Response Response,filterchain chain) {
  10. Request.requeststr = Request.getrequeststr (). Replace ("<", "[")
  11. . replace (">", "]--------htmlfilter");
  12. Chain.dofilter (Request, response, chain);
  13. Response.responsestr + = "--------htmlfilter";
  14. }
  15. }
Package com.lcq.filter;/** * Filter script elements in HTML * @author LCQ * */public class Htmlfilter implements filter {@Overridepublic Voi D DoFilter (Request request, Response Response,filterchain chain) {request.requeststr = Request.getrequeststr (). Replace ("<", "["). Replace (">", "]--------htmlfilter"); Chain.dofilter (request, response, chain); response.responsestr + = "--------Htmlfilter";}}

Sesitivefilter class:
[Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. Public class Sesitivefilter implements Filter {
  3. @Override
  4. public void DoFilter (Request request, Response Response, Filterchain chain) {
  5. Request.requeststr = Request.getrequeststr (). Replace ("sensitive", "" ")
  6. . replace ("Cat Cat", "haha------sesitivefilter");
  7. Chain.dofilter (Request, response, chain);
  8. Response.responsestr + = "------Sesitivefilter";
  9. }
  10. }
Package Com.lcq.filter;public Class Sesitivefilter implements filter {@Overridepublic void DoFilter (Request request, Response Response, Filterchain chain) {request.requeststr = Request.getrequeststr (). Replace ("sensitive", "  "). Replace (" Cat "," haha------sesitivefilter "); Chain.dofilter (request, response, chain); Response.responsestr + ="------ Sesitivefilter ";}}

Facefilter class:
[Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. Public class Facefilter implements Filter {
  3. @Override
  4. public void DoFilter (Request request, Response Response, Filterchain chain) {
  5. Request.requeststr = Request.getrequeststr (). Replace (":)",
  6. "^v^-------facefilter");
  7. Chain.dofilter (Request, response, chain);
  8. Response.responsestr + = "-------Facefilter";
  9. }
  10. }
Package Com.lcq.filter;public Class Facefilter implements filter {@Overridepublic void DoFilter (Request request, Response Response, Filterchain chain) {request.requeststr = Request.getrequeststr (). replace (":)", "^v^------- Facefilter "); Chain.dofilter (request, response, chain); Response.responsestr + ="-------Facefilter ";}}

Finally write the test class: [Java]View Plaincopyprint?
  1. Package com.lcq.filter;
  2. Public class Main {
  3. public static void Main (string[] args) {
  4. String message = "Sensitive vocabulary, Chongqing,<script> Hide and seek:)";
  5. Request Request = new request ();
  6. REQUEST.SETREQUESTSTR (message);
  7. Response Response = new Response ();
  8. RESPONSE.SETRESPONSESTR ("response");
  9. Filterchain FC = new Filterchain ();
  10. Fc.addfilter (new Htmlfilter ()). addfilter (new Sesitivefilter ());
  11. Filterchain FC2 = new Filterchain ();
  12. Fc2.addfilter (new Facefilter ());
  13. Fc.addfilter (FC2);
  14. Fc.dofilter (Request, RESPONSE,FC);
  15. System.out.println ("request =" + REQUEST.GETREQUESTSTR ());
  16. SYSTEM.OUT.PRINTLN ("response =" + RESPONSE.GETRESPONSESTR ());
  17. }
  18. }
Package Com.lcq.filter;public class Main {public static void main (string[] args) {String message = "sensitive vocabulary, Chongqing,<script> Hide and seek:) "; Request Request = new request (); REQUEST.SETREQUESTSTR (message); Response Response = new Response (); Response.setresponsestr ("Response"); Filterchain FC = new Filterchain (); Fc.addfilter (new Htmlfilter ()). addfilter (New Sesitivefilter ()); Filterchain FC2 = new Filterchain () Fc2.addfilter (New Facefilter ()); Fc.addfilter (FC2); Fc.dofilter (request, Response, FC); SYSTEM.OUT.PRINTLN ("request =" + REQUEST.GETREQUESTSTR ()); SYSTEM.OUT.PRINTLN ("response =" + RESPONSE.GETRESPONSESTR ());}}
Two places should be noted in the above example:

1. The filter interface is inherited in the filterchain we build, so the filterchain can be used like other filters in the test class, which greatly improves the flexibility.

2. For the implementation of the order of access processing of the responsibility chain, the problem is solved by using the idea of recursion, so that the first called node is reversed in the order in which it invokes the filter when it processes the return result. This solution is more common and ingenious in using struts and other frameworks to implement filters and interceptors.

Java design pattern-responsibility chain model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.