• 设计模式之过滤器模式


    博主上次分析完cas客户端源码后,发现了其实就是一个过滤器模式,对请求以及session一直做校验,每个过滤器都有各自的工作,互不影响耦合性低,所以就模仿其源码,做了一次过滤器模式的学习,图示如下

     其实过滤器整个类可以不用,只不过在jetty中,过滤器整合类不知有过滤器还有其他属性,比如config配置等。如下:参考即可

     好了,我们来看一下过滤器模式,一个过滤器接口定义方法,多个实现类去实现具体操作过滤,一个整合类增加一个过滤器,一个过滤器链表去实现以节点类型,依次进行过滤

    该接口进行定义方法:

     1 import java.io.IOException;
     2 import java.util.List;
     3 
     4 /**
     5  * @title: DemoFilter
     6  * @Author junyu
     7  * 旧巷里有一个穿着白衬衫笑起来如太阳般温暖我的少年。
     8  * 记忆里有一个穿着连衣裙哭起来如孩子般讨人喜的女孩。
     9  * 他说,哪年树弯了腰,人见了老,桃花落了白发梢,他讲的笑话她还会笑,那便是好。
    10  * 她说,哪年国改了号,坟长了草,地府过了奈何桥,她回头看时他还在瞧,就不算糟。
    11  * @Date: 2020/7/18 9:23
    12  * @Version 1.0
    13  */
    14 public interface DemoFilter{
    15 
    16     void doFilter(List list, DemoFilterChain demoFilterChain);
    17 }
    DemoFilter

    多个实现类:

     1 import java.util.List;
     2 
     3 /**
     4  * @title: AFilterImpl
     5  * @Author junyu
     6  * 旧巷里有一个穿着白衬衫笑起来如太阳般温暖我的少年。
     7  * 记忆里有一个穿着连衣裙哭起来如孩子般讨人喜的女孩。
     8  * 他说,哪年树弯了腰,人见了老,桃花落了白发梢,他讲的笑话她还会笑,那便是好。
     9  * 她说,哪年国改了号,坟长了草,地府过了奈何桥,她回头看时他还在瞧,就不算糟。
    10  * @Date: 2020/7/18 11:58
    11  * @Version 1.0
    12  */
    13 public class AFilterImpl implements DemoFilter{
    14 
    15     @Override
    16     public void doFilter(List list, DemoFilterChain demoFilterChain) {
    17         System.out.println("AFilterImpl is beginning");
    18         if (list.isEmpty()) {
    19             System.out.println("该列表为空,无需过滤!");
    20             return;
    21         }
    22         demoFilterChain.doFilter(list);
    23     }
    24 }
    AFilterImpl
     1 import java.util.List;
     2 
     3 /**
     4  * @title: BFilterImpl
     5  * @Author junyu
     6  * 旧巷里有一个穿着白衬衫笑起来如太阳般温暖我的少年。
     7  * 记忆里有一个穿着连衣裙哭起来如孩子般讨人喜的女孩。
     8  * 他说,哪年树弯了腰,人见了老,桃花落了白发梢,他讲的笑话她还会笑,那便是好。
     9  * 她说,哪年国改了号,坟长了草,地府过了奈何桥,她回头看时他还在瞧,就不算糟。
    10  * @Date: 2020/7/18 13:34
    11  * @Version 1.0
    12  */
    13 public class BFilterImpl implements DemoFilter{
    14 
    15     @Override
    16     public void doFilter(List list, DemoFilterChain demoFilterChain) {
    17         System.out.println("BFilterImpl is beginning");
    18         System.out.println("list`s body:");
    19         list.stream().forEach(one ->{
    20             System.out.print( one + ",");
    21         });
    22         demoFilterChain.doFilter(list);
    23     }
    24 }
    BFilterImpl

    一个整合类

     1 **
     2  * @title: FilterHolder
     3  * @Author junyu
     4  * 旧巷里有一个穿着白衬衫笑起来如太阳般温暖我的少年。
     5  * 记忆里有一个穿着连衣裙哭起来如孩子般讨人喜的女孩。
     6  * 他说,哪年树弯了腰,人见了老,桃花落了白发梢,他讲的笑话她还会笑,那便是好。
     7  * 她说,哪年国改了号,坟长了草,地府过了奈何桥,她回头看时他还在瞧,就不算糟。
     8  * @Date: 2020/7/18 9:21
     9  * @Version 1.0
    10  */
    11 public class FilterHolder{
    12     private transient DemoFilter filter;
    13 
    14     public DemoFilter getFilter() {
    15         return filter;
    16     }
    17 
    18     public void setFilter(DemoFilter filter) {
    19         this.filter = filter;
    20     }
    21 }
    FilterHolder

    一个过滤器链表

     1 import java.util.List;
     2 
     3 /**
     4  * @title: DemoFilterChain
     5  * @Author junyu
     6  * 旧巷里有一个穿着白衬衫笑起来如太阳般温暖我的少年。
     7  * 记忆里有一个穿着连衣裙哭起来如孩子般讨人喜的女孩。
     8  * 他说,哪年树弯了腰,人见了老,桃花落了白发梢,他讲的笑话她还会笑,那便是好。
     9  * 她说,哪年国改了号,坟长了草,地府过了奈何桥,她回头看时他还在瞧,就不算糟。
    10  * @Date: 2020/7/18 9:35
    11  * @Version 1.0
    12  */
    13 public class DemoFilterChain{
    14     FilterHolder filterHolder;
    15     DemoFilterChain next;
    16 
    17     DemoFilterChain(List<DemoFilter> list){
    18         if (list.isEmpty()) {
    19             return;
    20         }
    21         filterHolder = new FilterHolder();
    22         filterHolder.setFilter(list.get(0));
    23         list.remove(0);
    24         next = new DemoFilterChain(list);
    25     }
    26 
    27     public void doFilter(List list) {
    28         if (filterHolder != null) {
    29             DemoFilter filter = filterHolder.getFilter();
    30             filter.doFilter(list,next);
    31         }
    32     }
    33 }
    DemoFilterChain

    进行测试

     1 import java.util.ArrayList;
     2 import java.util.List;
     3 
     4 /**
     5  * @title: TestMain
     6  * @Author junyu
     7  * 旧巷里有一个穿着白衬衫笑起来如太阳般温暖我的少年。
     8  * 记忆里有一个穿着连衣裙哭起来如孩子般讨人喜的女孩。
     9  * 他说,哪年树弯了腰,人见了老,桃花落了白发梢,他讲的笑话她还会笑,那便是好。
    10  * 她说,哪年国改了号,坟长了草,地府过了奈何桥,她回头看时他还在瞧,就不算糟。
    11  * @Date: 2020/7/18 13:40
    12  * @Version 1.0
    13  */
    14 public class TestMain {
    15 
    16     public static void main(String[] args) {
    17         List<DemoFilter> list = new ArrayList<>();
    18         AFilterImpl aFilter = new AFilterImpl();
    19         BFilterImpl bFilter = new BFilterImpl();
    20         list.add(aFilter);
    21         list.add(bFilter);
    22         DemoFilterChain demoFilterChain = new DemoFilterChain(list);
    23 
    24         List<String> filterList = new ArrayList<>();
    25         filterList.add("this is filter string");
    26         filterList.add("this is filter end");
    27 
    28         demoFilterChain.doFilter(filterList);
    29 
    30     }
    31 }

    输出结果:

    AFilterImpl is beginning
    BFilterImpl is beginning
    list`s body:
    this is filter string,this is filter end,


    编写不易,如若转载请写明出处:https://www.cnblogs.com/guoxiaoyu/p/13335797.html

     

  • 相关阅读:
    asr相关技术总结
    SLURM 使用基础教程
    FSMN 及其变种 cFSMN DFSMN pyramidal-FSMN
    均方根误差(RMSE),平均绝对误差 (MAE),标准差 (Standard Deviation)
    linux文本编码格式转化 字幕处理
    PyTorch-Kaldi 语音识别工具包
    SRILM Ngram 折扣平滑算法
    awk 调用 shell 命令,并传递参数
    用 scikit-learn 和 pandas 学习线性回归
    逻辑回归 及 实例
  • 原文地址:https://www.cnblogs.com/guoxiaoyu/p/13335797.html
Copyright © 2020-2023  润新知