概念:为请求者创建了一个处理链,按序对该请求进行处理,在执行这条链的时候,可以选择是否继续向下执行或者直接返回(对一个进行进行多种规则的处理)
这种模式我觉得在实际 场景中应用比较少 他的关键点在于提供对后续处理者的引用
优点:
1、降低耦合度。它将请求的发送者和接收者解耦。
2、简化了对象。使得对象不需要知道链的结构。
3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。
4、增加新的请求处理类很方便。
案例:对字符窜的处理
// 1.定义抽象类接口
public abstract class FilterInterface {
protected String str;
//遵循里氏替换原则。
protected FilterInterface nextFilterInterface;
public void setNextFilter(FilterInterface nextFilterInterface) {
this.nextFilterInterface=nextFilterInterface;
}
//子类重写该方法 进行不同方式的处理
public abstract boolean updateData(String str);
}
//2.
public class String1Fileter extends FilterInterface {
private FilterInterface filterInterface;
@Override
public boolean updateData(String str) {
str=str+"string1";
System.out.println(str);
if(nextFilterInterface!=null){
nextFilterInterface.updateData(str);
}
return false;
}
}
//3.
public class StringFilter extends FilterInterface {
@Override
public boolean updateData(String str) {
str=str+"string";
System.out.println(str);
if(nextFilterInterface!=null){
nextFilterInterface.updateData(str);
}
return true;
}
}
//4.
public class String2Fileter extends FilterInterface{
@Override
public boolean updateData(String str) {
str=str+"string2";
System.out.println(str);
if(nextFilterInterface!=null){
nextFilterInterface.updateData(str);
}
return false;
}
public static void main(String[] args) {
StringFilter stringFilter=new StringFilter();
String1Fileter string1Fileter=new String1Fileter();
String2Fileter string2Fileter=new String2Fileter();
stringFilter.setNextFilter(string1Fileter);
string1Fileter.setNextFilter(string2Fileter);
stringFilter.updateData("1+");
}
}
马士兵责任链模式 仿照 过滤器实现的
@
1.用Filter模拟处理Request、Response
2.思路细节技巧:
(1)Filter的doFilter方法改为doFilter(Request,Resopnse,FilterChain),有FilterChain引用,为利用FilterChain调用下一个Filter做准备
(2)FilterChain继承Filter,这样,FilterChain既是FilterChain又是Filter,那么FilterChain就可以调用Filter的方法doFilter(Request,Resopnse,FilterChain)
(3)FilterChain的doFilter(Request,Resopnse,FilterChain)中,有index标记了执行到第几个Filter,当所有Filter执行完后request处理后,就会return,以倒序继续执行response处理
@
1.Filter.java
2.HTMLFilter.java
3.SensitiveFilter.jav
4.FilterChian.java
5.Request.java
6.Response.java
7.Test.java
1.Filter.java
1 public interface Filter {
2 public void doFilter(Request request, Response response, FilterChain filterChain);
3 }
2.HTMLFilter.java
1 public class HTMLFilter implements Filter {
2
3 @Override
4 public void doFilter(Request request, Response response,
5 FilterChain filterChain) {
6 request.setRequestStr(request.getRequestStr().replace('<', '[').replace(">", "]")+"---HTMLFilter()");
7 filterChain.doFilter(request, response, filterChain);
8 response.setResponseStr(response.getResponseStr()+"---HTMLFilter()");
9 }
10
11 }
3.SensitiveFilter.java
1 public class SensitiveFilter implements Filter {
2
3 @Override
4 public void doFilter(Request request, Response response,
5 FilterChain filterChain) {
6 request.setRequestStr(request.getRequestStr().replace("敏感", "幸福")+"---SensitiveFilter()");
7 filterChain.doFilter(request, response, filterChain);
8 response.setResponseStr(response.getResponseStr()+"---SensitiveFilter()");
9 }
10
11 }
4.FilterChian.java
1 public class FilterChain implements Filter {
2
3 private List<Filter> filters = new ArrayList<Filter>();
4 int index = 0; //标记执行到第几个filter
5
6 //把函数的返回值设为FilterChain,返回this,就能方便链式编写代码
7 public FilterChain addFilter(Filter filter) {
8 filters.add(filter);
9 return this;
10 }
11
12 public void doFilter(Request request, Response response, FilterChain fc) {
13 if(index == filters.size()) return ;
14 Filter f = filters.get(index);
15 index++;
16 f.doFilter(request, response, fc);
17 }
18
19 }
//5.Request.java
public class Request {
private String requestStr;
public String getRequestStr() {
return requestStr;
}
public void setRequestStr(String requestStr) {
this.requestStr = requestStr;
}
}
//6.Response.java
public class Response {
private String responseStr;
public String getResponseStr() {
return responseStr;
}
public void setResponseStr(String responseStr) {
this.responseStr = responseStr;
}
}
7.Test.java
public class Test {
@org.junit.Test
public void testFilter(){
String msg = "<html>敏感字眼</html>";
Request request = new Request();
request.setRequestStr(msg);
Response response = new Response();
response.setResponseStr("response------------");
FilterChain fc = new FilterChain();
fc.addFilter(new HTMLFilter()).addFilter(new SensitiveFilter());
fc.doFilter(request, response, fc);
System.out.println(request.getRequestStr());
System.out.println(response.getResponseStr());
}
}