Java design pattern: responsibility chain

Source: Internet
Author: User

The purpose of the Chain of Responsibility model is to allow multiple objects to process requests, so as to avoid coupling between request senders and receivers. Connect these objects into a chain and pass requests along the chain until an object processes it.

When processing user requests, you may need to add different processing logic to the requests based on different situations. At this time, you can use the responsibility chain for design. When you need to add a processing logic, you can easily add a processing node.

Now, we need to process user requests, process the string information submitted by the user layer by layer, and process the returned string layer by layer when the processing is complete and the returned result is returned, when processing the returned results, the processing order is the opposite of the previous one.

First, create a user Request and receive Object Request and Response:

Package com. lcq. filter;
 
Public class Request {
String requestStr;
 
Public String getRequestStr (){
Return requestStr;
}
 
Public void setRequestStr (String requestStr ){
This. requestStr = requestStr;
}
 
}
Package com. lcq. filter;
 
Public class Response {
String responseStr;
 
Public String getResponseStr (){
Return responseStr;
}
 
Public void setResponseStr (String responseStr ){
This. responseStr = responseStr;
}
 
}
We abstract the logic for processing user information into filters to further abstract the Filter interface:

Package com. lcq. filter;
 
Public interface Filter {
Public void doFilter (Request request, Response response, FilterChain chain );
 
}
Note that the doFilter method parameter in the Filte Interface contains a variable of FilterChain. We will create the FilterChain class:

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. filters. add (f );
Return this;
}
 
@ Override
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 );
}
}
FilterChain inherits the Filter interface to implement the doFilter method. In FilterChain, another index variable is used to mark which Filter is currently accessed, these filters are stored in the ArrayList, so that users can implement their own filters when using them, write their own processing logic, and then add their own filters to the ArrayList, call the doFilter method of FilterChain to traverse the entire responsibility chain.

Below we write three filters:

HTMLFilter class:

Package com. lcq. filter;
 
/**
* Filter script elements in HTML
* @ Author lcq
*
*/
Public class HTMLFilter implements Filter {
 
@ Override
Public void doFilter (Request request, Response response, FilterChain chain ){
Request. requestStr = request. getRequestStr (). replace ("<","[")
. Replace (">", "] -------- HTMLFilter ");
Chain. doFilter (request, response, chain );
Response. responseStr + = "-------- HTMLFilter ";

}
 
}
SesitiveFilter class:

Package com. lcq. filter;
 
Public class SesitiveFilter implements Filter {
 
@ Override
Public 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:

Package com. lcq. filter;
 
Public class FaceFilter implements Filter {
 
@ Override
Public 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:

Package com. lcq. filter;
 
Public class Main {
Public static void main (String [] args ){
String message = "sensitive words, Chongqing, <script> Dodge :)";
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 ());
}
 
}
Note the following two points in the above instance:

1. The FilterChain we created inherits the Filter interface, so in the test class, we can use FilterChain like other filters, greatly improving the flexibility;

2. the recursive method is used to solve the access processing sequence problem of the responsible chain, so that the order in which the first called node calls the filter when processing the returned results is the opposite. This solution is widely used in Struts and other frameworks to implement filters and interceptors, and is very clever.

Related Article

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.