博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java设计模式--责任链模式
阅读量:5040 次
发布时间:2019-06-12

本文共 5103 字,大约阅读时间需要 17 分钟。

责任链模式:将能够处理同一类请求的对象连成一条链,如果不能处理则传递给链上的下一个对象。

//请假对象public class QingjiaRequest {        private String name;        private int days;        private String reason;    public QingjiaRequest(String name, int days, String reason) {        this.name = name;        this.days = days;        this.reason = reason;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getDays() {        return days;    }    public void setDays(int days) {        this.days = days;    }    public String getReason() {        return reason;    }    public void setReason(String reason) {        this.reason = reason;    }}
//抽象类public abstract class Leader {        String name;        Leader nextLeader;    public Leader(String name) {        this.name = name;    }    public void setNextLeader(Leader nextLeader) {        this.nextLeader = nextLeader;    }        //设置责任链的处理对象(核心)    public abstract void handleRequest(QingjiaRequest qingjiaRequest);}
//主任public class Zhuren extends Leader{    public Zhuren(String name){        super(name);    }            public void handleRequest(QingjiaRequest qingjiaRequest) {        if(qingjiaRequest.getDays()<3){            System.out.println("员工请假小于3天");            System.out.println("审批通过");        }else{            if(this.nextLeader!=null){                this.nextLeader.handleRequest(qingjiaRequest);            }        }                    }}
//经理public class Manager extends Leader{    public Manager(String name){        super(name);    }            public void handleRequest(QingjiaRequest qingjiaRequest) {        if(qingjiaRequest.getDays()<10&&qingjiaRequest.getDays()>3){            System.out.println("经理审批通过");        }else{             //经理处理后的逻辑        }                    }}
public static void main(String[] args) {                QingjiaRequest qingjiaRequest = new QingjiaRequest("张三", 2, "请假");        //QingjiaRequest qingjiaRequest1 = new QingjiaRequest("张三", 5, "请假");        Leader a = new Zhuren("张主任");        Leader b = new Manager("王经理");         //可以用一个容器对象责任链对象进行一些封装        a.setNextLeader(b);        //开始请假        a.handleRequest(qingjiaRequest);            }

应用场景:try catch,javascript 冒泡,servlet filter,struts2 拦截器

 

模仿 JavaWeb 中的 Filter,可以对其思路有个很好的把握

1.Filter
package com.cun.filters;
import com.cun.main.FilterChain;
import com.cun.main.Request;
import com.cun.main.Response;
public interface Filter {
    void doFilter(Request request, Response response, FilterChain chain);
}
 
2.FirstFilter
package com.cun.filters;
import com.cun.main.FilterChain;
import com.cun.main.Request;
import com.cun.main.Response;
public class FirstFilter implements Filter {
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        request.doSomething(this.getClass().getName());
        // 先执行所有request再倒序执行所有response
        chain.doFilter(request, response, chain);
        response.doSomething(this.getClass().getName());
    }
}
3.SecondFilter
package com.cun.filters;
import com.cun.main.FilterChain;
import com.cun.main.Request;
import com.cun.main.Response;
public class SecondFilter implements Filter {
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        request.doSomething(this.getClass().getName());
        chain.doFilter(request, response, chain);
        response.doSomething(this.getClass().getName());
    }
}
  
3.ThirdFilter
package com.cun.filters;
import com.cun.main.FilterChain;
import com.cun.main.Request;
import com.cun.main.Response;
public class ThirdFilter implements Filter {
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        request.doSomething(this.getClass().getName());
        chain.doFilter(request, response, chain);
        response.doSomething(this.getClass().getName());
    }
}
4.Request
package com.cun.main;
public class Request {
    public void doSomething(String str) {
        System.out.println("Request ---> " + str);
    }
}
5.Response
package com.cun.main;
public class Response {
    public void doSomething(String str) {
        System.out.println("Response ---> " + str);
    }
}
  
6.FilterChain
package com.cun.main;
import com.cun.filters.Filter;
import java.util.ArrayList;
import java.util.List;
public class FilterChain implements Filter {
    private List<Filter> filters = new ArrayList<Filter>();
    private int index = 0;
    // 链式调用
    public FilterChain addFilter(Filter filter) {
        this.filters.add(filter);
        return this;
    }
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        if (index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(request, response, chain);
    }
}
   
7.FilterTest 测试
package com.cun.main;
import com.cun.filters.FirstFilter;
import com.cun.filters.SecondFilter;
import com.cun.filters.ThirdFilter;
import org.junit.Test;
public class FilterTest {
    @Test
    public void Test(){
        FilterChain filterChain = new FilterChain();
        filterChain.addFilter(new FirstFilter()).addFilter(new SecondFilter()).addFilter(new ThirdFilter());
        filterChain.doFilter(new Request(),new Response(),filterChain);
    }
}

 

转载于:https://www.cnblogs.com/jentary/p/5922096.html

你可能感兴趣的文章
list-style-type -- 定义列表样式
查看>>
hibernate生成表时,有的表可以生成,有的却不可以 2014-03-21 21:28 244人阅读 ...
查看>>
mysql-1045(28000)错误
查看>>
Ubuntu 编译出现 ISO C++ 2011 不支持的解决办法
查看>>
1.jstl c 标签实现判断功能
查看>>
Linux 常用命令——cat, tac, nl, more, less, head, tail, od
查看>>
超详细的Guava RateLimiter限流原理解析
查看>>
VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
查看>>
Halcon一日一练:图像拼接技术
查看>>
Swift - RotateView
查看>>
iOS设计模式 - 中介者
查看>>
centos jdk 下载
查看>>
HDU 1028 Ignatius and the Princess III(母函数)
查看>>
关于多路复用器的综合结果
查看>>
(转)面向对象最核心的机制——动态绑定(多态)
查看>>
token简单的使用流程。
查看>>
django创建项目流程
查看>>
UIActionSheet 修改字体颜色
查看>>
Vue 框架-01- 入门篇 图文教程
查看>>
Spring注解之@Lazy注解,源码分析和总结
查看>>