设计模式-责任链

以下是根据文章内容生成的Java责任链设计模式的代码示例,包含两种不同的使用场景:

场景一:请求过滤

  1. 抽象处理器
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public interface MyFilter {
    boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain);
}
  1. 具体处理器 - 登录过滤器
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class LoginFilter implements MyFilter {
    @Override
    public boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {
        String reqMsg = request.getReqMsg();
        if (!reqMsg.contains("username") ||!reqMsg.contains("password")) {
            System.out.println("登录失败,请重新登录");
            return false;
        }
        String resMsg = response.getResMsg();
        resMsg += "--登录过滤器返回响应消息";
        response.setResMsg(resMsg);
        return true;
    }
}
  1. 具体处理器 - 业务过滤器
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class BusinessFilter implements MyFilter {
    @Override
    public boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {
        String reqMsg = request.getReqMsg();
        if (!reqMsg.contains("/filter")) {
            System.out.println("请求的接口路径不存在");
            return false;
        }
        String resMsg = response.getResMsg();
        resMsg += "--业务逻辑处理完返回响应消息";
        response.setResMsg(resMsg);
        return true;
    }
}
  1. 处理器链
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
import java.util.ArrayList;
import java.util.List;

public class MyFilterChain {
    private List<MyFilter> filterList = new ArrayList<>();

    public MyFilterChain addFilter(MyFilter filter) {
        filterList.add(filter);
        return this;
    }

    public void doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {
        for (MyFilter filter : filterList) {
            if (!filter.doFilter(request, response, chain)) {
                break;
            }
        }
    }
}
  1. 实体类 - 请求类
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class MyRequest {
    private String reqMsg;

    public String getReqMsg() {
        return reqMsg;
    }

    public void setReqMsg(String reqMsg) {
        this.reqMsg = reqMsg;
    }
}
  1. 实体类 - 响应类
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class MyResponse {
    private String resMsg;

    public String getResMsg() {
        return resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }
}
  1. 测试类
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class RespChainTest01 {
    public static void main(String[] args) {
        String reqMsg = "{username:andy,password:123456,url:\"http://ip:port/filter\"}";
        MyRequest request = new MyRequest();
        request.setReqMsg(reqMsg);
        MyResponse response = new MyResponse();
        MyFilterChain chain = new MyFilterChain();
        MyFilter loginFilter = new LoginFilter();
        MyFilter businessFilter = new BusinessFilter();
        chain.addFilter(loginFilter).addFilter(businessFilter);
        chain.doFilter(request, response, chain);
        String resMsg = response.getResMsg();
        System.out.println("得到响应结果=======" + resMsg);
    }
}

场景二:请假审批

  1. 实体类 - 请假请求类
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class LeaveRequest {
    private Integer days;
    private String reason;

    public LeaveRequest() {
    }

    public Integer getDays() {
        return days;
    }

    public void setDays(Integer days) {
        this.days = days;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    @Override
    public String toString() {
        return "Leave{" +
                "days=" + days +
                ", reason='" + reason + '\'' +
                '}';
    }
}
  1. 实体类 - 审批结果类
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class Result {
    private boolean isRatify;
    private String info;

    public boolean isRatify() {
        return isRatify;
    }

    public void setRatify(boolean ratify) {
        isRatify = ratify;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Result{" +
                "isRatify=" + isRatify +
                ", info='" + info + '\'' +
                '}';
    }
}
  1. 抽象处理器
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public interface Ratify {
    Result handle(LeaveRequest request, Result result, MyChain chain);
}
  1. 具体处理器 - 组长审批
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class GroupLeader implements Ratify {
    @Override
    public Result handle(LeaveRequest request, Result result, MyChain chain) {
        Integer days = request.getDays();
        if (days <= 2) {
            result.setRatify(true);
            result.setInfo("组长同意,早去早回。");
        } else {
            chain.doFilter(request, result, chain);
        }
        return result;
    }
}
  1. 具体处理器 - 主管审批
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class DepartmentHead implements Ratify {
    @Override
    public Result handle(LeaveRequest request, Result result, MyChain chain) {
        Integer days = request.getDays();
        if (days <= 4) {
            result.setRatify(true);
            result.setInfo("主管同意,路上注意安全。");
        } else {
            chain.doFilter(request, result, chain);
        }
        return result;
    }
}
  1. 具体处理器 - 经理审批
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class DepartmentManager implements Ratify {
    @Override
    public Result handle(LeaveRequest request, Result result, MyChain chain) {
        Integer days = request.getDays();
        if (days <= 7) {
            result.setRatify(true);
            result.setInfo("经理同意,回去安心办事。");
        } else {
            result.setRatify(false);
            result.setInfo("经理不同意,时间太久。");
            chain.doFilter(request, result, chain);
        }
        return result;
    }
}
  1. 处理器链
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
import java.util.ArrayList;
import java.util.List;

public class MyChain {
    private int index = 0;
    private List<Ratify> filterList = new ArrayList<>();

    public MyChain addFilter(Ratify ratify) {
        filterList.add(ratify);
        return this;
    }

    public void doFilter(LeaveRequest request, Result result, MyChain chain) {
        if (index == filterList.size()) {
            return;
        }
        Ratify ratify = filterList.get(index);
        index++;
        ratify.handle(request, result, chain);
    }
}
  1. 测试类
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class RespChainTest02 {
    public static void main(String[] args) {
        LeaveRequest request = new LeaveRequest();
        request.setDays(3);
        Result result = new Result();
        MyChain chain = new MyChain();
        Ratify leader = new GroupLeader();
        Ratify head = new DepartmentHead();
        Ratify manager = new DepartmentManager();
        chain.addFilter(leader).addFilter(head).addFilter(manager);
        chain.doFilter(request, result, chain);
        System.out.println("请假结果======" + result);
    }
}
相关推荐
草莓base11 分钟前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Allen Bright24 分钟前
maven概述
java·maven
编程重生之路27 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱27 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
努力进修36 分钟前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list
politeboy36 分钟前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
Daniel 大东1 小时前
BugJson因为json格式问题OOM怎么办
java·安全
哪 吒6 小时前
最简单的设计模式,抽象工厂模式,是否属于过度设计?
设计模式·抽象工厂模式
Theodore_10226 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸7 小时前
01-spring security认证笔记
java·笔记·spring