抽丝剥茧设计模式- 责任链模式应用场景

  Servlet中应用到责任链模式,下面是仿照实现的代码,请享用!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.design.behavioral.chain.v4;
 
import java.util.ArrayList;
import java.util.List;
 
/*
需求:
    处理request,response
    请求过来的时候是HtmlFilter处理、SensitiveFilter处理、ScriptFilter处理。
    响应返回的时候是ScriptFilter处理、SensitiveFilter处理、HtmlFilter处理。
 */
public class ServletFilter {
    public static void main(String[] args) {
        Request request = new Request();
        request.requestStr = "I am request ";
        Response response = new Response();
        response.responseStr = "I am response ";
        FilterChain filterChain = new FilterChain();
        filterChain.add(new HtmlFilter()).add(new SensitiveFilter()).add(new ScriptFilter());
        filterChain.doFilter(request, response);
        System.out.println(request.requestStr);
        System.out.println(response.responseStr);
    }
}
 
class Request {
    String requestStr;
}
 
class Response {
    String responseStr;
}
 
interface Filter {
    boolean doFilter(Request request, Response response, FilterChain filterChain);
}
 
class HtmlFilter implements Filter {
    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        request.requestStr = request.requestStr + " | HtmlFilter deal!";
        filterChain.doFilter(request, response);
        response.responseStr = response.responseStr + " | HtmlFilter deal!";
        return true;
    }
}
 
class SensitiveFilter implements Filter {
    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        request.requestStr = request.requestStr + " | SensitiveFilter deal!";
        filterChain.doFilter(request, response);
        response.responseStr = response.responseStr + " |  SensitiveFilter deal!";
        return true;
    }
}
 
class ScriptFilter implements Filter {
    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        request.requestStr = request.requestStr + " | ScriptFilter deal!";
        filterChain.doFilter(request, response);
        response.responseStr = response.responseStr + " | ScriptFilter deal!";
        return true;
    }
}
 
class FilterChain {
    List<Filter> filters = new ArrayList<Filter>();
    int index = 0;
 
    FilterChain add(Filter filter) {
        this.filters.add(filter);
        return this;
    }
 
    public boolean doFilter(Request request, Response response) {
        if (index == filters.size()) return false;
        Filter f = filters.get(index);
        index++;
        f.doFilter(request, response, this);
        return true;
    }
}

  

感谢阅读到现在,请在留言区提出宝贵的意见!

更多精彩内容,关注微信公众号:技术严选

posted @   YolandaRay  阅读(110)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示