Responsibility Chain Design Model

Source: Internet
Author: User

package com.huawei.oss.filter;

Public interface Filter {

void Dofilter (Request request,response response,filterchain chain);
}

package com.huawei.oss.filter;

Public class Facefilter implements Filter {

 

public void Dofilter (Request request, Response Response,filterchain chain) {
Request.requeststr=request.requeststr.replace ("<", "{"). Replace (">", "}") + "---->facefilter";
Chain.dofilter (Request,response,chain);
Response.responsestr + = "---->facefilterresponse";

}

}

package com.huawei.oss.filter;

Public class Htmlfilter implements Filter {

 

public void Dofilter (Request request, Response Response,filterchain chain) {
Request.requeststr=request.requeststr.replace ("Java", "O (∩_∩) o~") + "---->htmlfilterrequest";
Chain.dofilter (Request,response,chain);
Response.responsestr + = "--->htmlfilterresponse";
}

}

package com.huawei.oss.filter;

Public class Sessionfilter implements filter{


public void Dofilter (Request request, Response Response,filterchain chain) {
Request.requeststr=request.requeststr.replace ("Job search", "employment") + "--------->sessionfilter";
Chain.dofilter (Request,response,chain);
Response.responsestr + = "---->sessionfilterresponse";
}

}

Package com.huawei.oss.filter;

Import java.util.*;

public class Filterchain implements Filter {

private int index = 0;

Private list<filter> filters = new arraylist<filter> ();

Public Filterchain addfilter (Filter f)
{
This.filters.add (f);
return this;
}

public void Dofilter (Request request, Response Response,filterchain chain)
{
if (index = = filters.size ())
{
Return
}

Filter f = filters.get (index);
Index + +;
F.dofilter (Request,response,chain);
}

}

Package com.huawei.oss.filter;

public class Request {
String Requeststr;

Public String Getrequeststr () {
return requeststr;
}

public void Setrequeststr (String requeststr) {
This.requeststr = Requeststr;
}

}

Package com.huawei.oss.filter;

public class Response {
String Responsestr;

Public String Getresponsestr () {
return responsestr;
}

public void Setresponsestr (String responsestr) {
This.responsestr = Responsestr;
}

}

Package com.huawei.oss.filter;

public class Test {

/**
* @param args
*/
public static void Main (string[] args) {
String msg = "Now the society is difficult to find a job,<java> to find work wages high";

Request Request = new request ();
REQUEST.SETREQUESTSTR (msg);

Response Response = new Response ();
RESPONSE.SETRESPONSESTR ("response");
Filterchain chain = new Filterchain ();
Chain.addfilter (New Htmlfilter ())
. addfilter (New Facefilter ())
. addfilter (New Sessionfilter ());
Chain.dofilter (Request,response,chain);
System.out.println (Request.getrequeststr ());
System.out.println (Response.getresponsestr ());


}

}

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.