设计模式-责任链

以下是根据文章内容生成的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);
    }
}
相关推荐
代码之光_19807 分钟前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端
ajsbxi12 分钟前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
StayInLove31 分钟前
G1垃圾回收器日志详解
java·开发语言
对许35 分钟前
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“
java·log4j
无尽的大道39 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
小鑫记得努力1 小时前
Java类和对象(下篇)
java
binishuaio1 小时前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE1 小时前
【Java SE】StringBuffer
java·开发语言
老友@1 小时前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
wrx繁星点点1 小时前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式