设计模式——责任链
Posted 我的梦想是宇宙和平
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式——责任链相关的知识,希望对你有一定的参考价值。
先讲个故事吧
那天注定是历史性的一天,K获取到了某不知名996企业的不知名接口,作为一个长期工作在996制度之上的程序员,K不满于当下广大程序员正在经历的996工作制,准备掀起反对996的风潮,”今天就从这里开始吧!“,K的眼里闪烁起自由的光芒,他编写了一串黑客代码,并附上了一句话——”反对996,支持955!“,通过获取到的接口将编写的请求数据发送到了996的企业服务器。
然而意想不到的是,K的黑客代码和反对标语并没有生效,那个996企业的网站首页上还是印着”坚持996,奋斗到退休“的鲜红标语。过了一会,K的屏幕上出现了接口的回调:
”反对955,支持996!"。
——至此,反对996计划宣告失败。
责任链?
-“我到现在还是不知道我为什么会失败。”
-”责任链?这是什么东西??“
-”你发过去的请求都被人家处理了,懂不?“
-”是我的代码被过滤了吗,像这样?“
public class Main {
public static void main(String[] args) {
Msg msg = new Msg();
msg.setMsg("反对996,支持955!");
Filter filter = new SensitiveFilter ();
filter .doFilter(msg);
System.out.println(msg);
}
}
class Msg {
String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return msg;
}
}
interface Filter {
boolean doFilter(Msg m);
}
class SensitiveFilter implements Filter {
@Override
public boolean doFilter(Msg m) {
String r = m.getMsg();
r = r.replaceAll("反对996,支持955!", "反对955,支持996!");
m.setMsg(r);
return false;
}
}
“差不多,但也差很多,责任链模式是为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦......你小子要学的还多着呢......这串代码你先拿去跑跑看,下次听我给你仔细分析分析。“
public class Main {
public static void main(String[] args) {
Request request = new Request();
Response response = new Response();
htmlFilter1 htmlFilter1 = new HTMLFilter1();
HTMLFilter2 htmlFilter2 = new HTMLFilter2();
SensitiveFilter1 sensitiveFilter1 = new SensitiveFilter1();
SensitiveFilter2 sensitiveFilter2 = new SensitiveFilter2();
FilterChain filterChain1 = new FilterChain();
FilterChain filterChain2 = new FilterChain();
filterChain1.addFilter(htmlFilter1);
filterChain1.addFilter(htmlFilter2);
filterChain2.addFilter(sensitiveFilter1);
filterChain2.addFilter(sensitiveFilter2);
filterChain1.addFilter(filterChain2);
filterChain1.doFilter(request,response,filterChain1);
}
}
class Request{
String str;
}
class Response{
String str;
}
interface Filter{
boolean doFilter(Request request,Response response,FilterChain filterChain);
}
class HTMLFilter1 implements Filter{
@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain) {
request.str = "request filter html1";
System.out.println(request.str);
filterChain.doFilter(request,response,filterChain);
response.str = "response filter html1";
System.out.println(response.str);
return false;
}
}
class HTMLFilter2 implements Filter{
@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain) {
request.str = "request filter html2";
System.out.println(request.str);
filterChain.doFilter(request,response,filterChain);
response.str = "response filter html2";
System.out.println(response.str);
return false;
}
}
class SensitiveFilter1 implements Filter{
@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain) {
request.str = "request filter sensitive1";
System.out.println(request.str);
filterChain.doFilter(request,response,filterChain);
response.str = "response filter sensitive1";
System.out.println(response.str);
return false;
}
}
class SensitiveFilter2 implements Filter{
@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain) {
request.str = "request filter sensitive2";
System.out.println(request.str);
filterChain.doFilter(request,response,filterChain);
response.str = "response filter sensitive2";
System.out.println(response.str);
return false;
}
}
class FilterChain implements Filter{
List<Filter> filters = new ArrayList<>();
int index = 0;
@Override
public boolean doFilter(Request request, Response response,FilterChain filterChain) {
if (index==filters.size()) return false;
Filter f = filters.get(index);
index++;
f.doFilter(request,response,this);
return false;
}
public FilterChain addFilter(Filter filter){
filters.add(filter);
return this;
}
}
Ps:由于责任链难度较大,所以首先建议先运行代码观察程序的运行流程,到底”反对955,支持996!"是如何变成”反对996,支持955!"的呢?具体的实现细节将在后续加以补充~
以上是关于设计模式——责任链的主要内容,如果未能解决你的问题,请参考以下文章