Design pattern-Responsibility chain design

Source: Internet
Author: User

Look at the design pattern of the responsibility chain design mode, the main implementation of similar interceptors function, write down to facilitate their review later.

1. Application Scenario

If you want to deal with the crawling crawl content, you need to send these things to de-noising, such as: Take formatting HTML tags, go to page scripts, to sensitive information, and so on, if the filter content is written in a method, it is bound to create a strong code coupling, if you continue to add content, will cause great maintenance costs.

Therefore, we need to extract the content of the change to define the interface, we need to use it only to achieve its corresponding operation.

2. Example application 1, Content denoising filter

Define the filter interface first

Package org.andy.filter;/** * Created: 2015-1-25 pm 10:55:03 *  * @author Andy * @version 2.2 */public interface Filter {/** * Define Filter * @return */public string doFilter (string src);}

Define several filters to achieve the specific filtering function.

HTML Content filtering:

Package org.andy.filter;/**   * Created: 2015-1-25 pm 10:56:41   * @author Andy * @version 2.2   */public class Htmlfilter implements filter {@Overridepublic string doFilter (string src) {//Implements filtering of HTML content return Src.replace ("<", "["). Replace (">", "]"); }}

JavaScript Script filtering:

Package org.andy.filter;/**   * Created: 2015-1-25 pm 11:01:24   * @author Andy * @version 2.2   */public class Javascriptfilter implements filter {@Overridepublic string doFilter (string src) {//Implement filter JavaScript script return Src.replace ( "JavaScript", "JS");}}

Sensitive content filtering:

Package org.andy.filter;/**   * Created: 2015-1-25 pm 11:09:00   * @author Andy * @version 2.2   */public class Sensitivefilter implements filter{@Overridepublic string DoFilter (string src) {//Handles sensitive content return Src.replace ("Tease fork", "* *");}}

Define the chain of responsibility, the filter chain:

Package Org.andy.filter;import Java.util.arraylist;import java.util.list;/**   * Created: 2015-1-25 pm 11:14:58   * @ Author Andy * @version 2.2   * Responsibility Chain */public class Filterchain implements Filter {private list<filter> filters = NE W arraylist<filter> ();p ublic Filterchain addfilter (filter filter) {This.filters.add (filter);//return filter chain return this;} @Overridepublic string DoFilter (string src) {for (Filter filter:filters) {src = filter.dofilter (src);} return src;}}

Test Content filtering:

Package org.andy.filter;/** * Created: 2015-1-25 pm 11:12:49 *  * @author Andy * @version 2.2 */public class Testfilter {/** * @param args */public static void main (string[] args) {String src = "

2, to achieve the interception of the Web filter

Filter interception in Java EE, when requests are requested, all requests are executed first, and the response content is filtered in response.

Define Request:

Package org.andy.web.filter;/** * Created: 2015-1-25 pm 11:47:06 *  * @author Andy * @version 2.2 */public class Request {
   string Requeststr;}

Define response:

Package org.andy.web.filter;/** * Created: 2015-1-25 pm 11:48:09 *  * @author Andy * @version 2.2 */public class Response {S Tring Responsestr;}

To define an Interceptor interface:

Package org.andy.web.filter;/** * Created: 2015-1-25 pm 11:45:31 *  * @author Andy * @version 2.2  *  Filter interface, Respond to request and Response requests */public interface Filter {public void DoFilter (Request request, Response Response, Filterchain Cha in);}

Define the chain of responsibility:

Package Org.andy.web.filter;import Java.util.arraylist;import java.util.list;/** * created: 2015-1-26 morning 10:07:08 *  @ Author Andy * @version 2.2 */public class Filterchain implements Filter {private list<filter> filters = new Arraylis T<filter> ();p rivate int index;public Filterchain addfilter (filter filter) {This.filters.add (filter); return this ;}  @Overridepublic void DoFilter (Request request, Response Response, Filterchain chain) {//TODO auto-generated method stubif (Index = = filters.size ()) return; Filter filter = Filters.get (index); Index++;filter.dofilter (request, response, chain);}}

To define HTML blocking:

Package org.andy.web.filter;/**   * Created: 2015-1-25 pm 10:56:41   * @author Andy * @version 2.2   */public class Htmlfilter implements filter{@Overridepublic void DoFilter (Request request, Response Response, Filterchain chain) {//Execute First The request is then executed responserequest.requeststr = Request.requestStr.replace ("<", "["). Replace (">", "]") + "----- Htmlfilterrequest ";//Perform the next chain of Filterchain.dofilter (request, response, chain); Response.responsestr + ="----- Htmlfilterresponse "; }}

Define JavaScript Script filtering:

Package org.andy.web.filter;/**   * Created: 2015-1-25 pm 11:01:24   * @author Andy * @version 2.2   */public class Javascriptfilter implements Filter {@Overridepublic void DoFilter (Request request, Response Response, Filterchain chain) {//Execute request before executing RESPONSEREQUEST.REQUESTSTR = Request.requestStr.replace ("javascript", "JS") + "----- Javascriptfilterrequest ";//Perform the next chain of Filterchain.dofilter (request, response, chain); Response.responsestr + ="----- Javascriptfilterresponse ";}}

Interception test:

Package org.andy.web.filter;/**   * created: 2015-1-26 a.m. 11:10:11   * @author Andy * @version 2.2   */public class Testfilter {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubstring src = "&l T;html/> <javascript>alert (' You are a Tease fork ');</javascript> "; SYSTEM.OUT.PRINTLN (SRC); Request Request = new request (); request.requeststr = src; Response Response = new Response (); response.responsestr = "Response"; Filterchain chain = new Filterchain (); Chain.addfilter (new Htmlfilter ())         . addfilter (New Javascriptfilter ()); Chain.dofilter (Request, response, chain); System.out.println (REQUEST.REQUESTSTR); System.out.println (RESPONSE.RESPONSESTR);}}










Design pattern-Responsibility chain design

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.