Featured image of post Java设计模式-职责链模式ChainOfResponsibility

Java设计模式-职责链模式ChainOfResponsibility

相关文章

【合集】Java设计模式

应用

{@link jakarta.servlet.FilterChain}抽象处理器

职责链模式

请假流程系统:1天小组长审批通过即可,23天小组长+部门经理审批通过,37小组长+部门经理+总经理审批通过

请假条类

1
2
3
4
5
6
7
@Getter
@AllArgsConstructor
public static class LeaveRequest {
    private String name;
    private int num;
    private String content;
}

抽象处理者

 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
@Setter
public static abstract class Handler {
    protected final static int NUMBER_ONE = 1;
    protected final static int NUMBER_THREE = 3;
    protected final static int NUMBER_SEVEN = 7;

    private final int numStart;
    private final int numEnd;
    private Handler nextHandler;

    public Handler(int numStart, int numEnd) {
        this.numStart = numStart;
        this.numEnd = numEnd;
    }

    public final void submit(LeaveRequest leave) {
        if (leave.getNum() == 0) return;
        if (leave.getNum() >= this.numStart) {
            this.handleLeave(leave);
            if (leave.getNum() > numEnd && this.nextHandler != null) {
                this.nextHandler.submit(leave);
            } else {
                System.out.println("流程结束");
            }
        }
    }
    protected abstract void handleLeave(LeaveRequest leave);
}

具体处理者

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static class GroupLeader extends Handler {
    public GroupLeader() {
        super(0, NUMBER_ONE);
    }
    protected void handleLeave(LeaveRequest leave) {
        System.out.println("小组长审批:同意");
    }
}
public static class Manager extends Handler {
    public Manager() {
        super(NUMBER_ONE, NUMBER_THREE);
    }
    protected void handleLeave(LeaveRequest leave) {
        System.out.println("部门经理审批:同意");
    }
}
public static class GeneralManager extends Handler {
    public GeneralManager() {
        super(NUMBER_THREE, NUMBER_SEVEN);
    }
    protected void handleLeave(LeaveRequest leave) {
        System.out.println("总经理审批:同意");
    }
}

客户端调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
LeaveRequest leave = new LeaveRequest("小花", 5, "身体不适");
System.out.println(leave.getName() + "请假" + leave.getNum() + "天," + leave.getContent() + "。");

// 如果领导固定,可放在领导实现类中。
GeneralManager generalManager = new GeneralManager();

Manager manager = new Manager();
manager.setNextHandler(generalManager);

GroupLeader groupLeader = new GroupLeader();
groupLeader.setNextHandler(manager);

// 提交给小组长
groupLeader.submit(leave);
System.out.println("-----------------------------------------------");

// 提交给部门经理
manager.submit(leave);
System.out.println("-----------------------------------------------");

// 提交给总经理
generalManager.submit(leave);

输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
小花请假5天,身体不适。
小组长审批:同意
部门经理审批:同意
总经理审批:同意
流程结束
-----------------------------------------------
部门经理审批:同意
总经理审批:同意
流程结束
-----------------------------------------------
总经理审批:同意
流程结束

模拟FilterChain

模拟Request、Response

1
2
3
4
public interface Request {
}
public interface Response {
}

模拟FilterChain

1
2
3
public interface Filter {
    void doFilter(Request req, Response res, FilterChain c);
}

模拟实现具体过滤器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public static class FirstFilter implements Filter {
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        System.out.println("过滤器1 前置处理");
        // 先执行所有request再倒序执行所有response
        chain.doFilter(request, response);
        System.out.println("过滤器1 后置处理");
    }
}
public static class SecondFilter implements Filter {
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        System.out.println("过滤器2 前置处理");
        // 先执行所有request再倒序执行所有response
        chain.doFilter(request, response);
        System.out.println("过滤器2 后置处理");
    }
}

模拟实现过滤器链 FilterChain

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public static class FilterChain {
    private final List<Filter> filters = new ArrayList<>();
    private int index = 0;

    public FilterChain addFilter(Filter filter) {
        filters.add(filter);
        return this;
    }

    public void doFilter(Request request, Response response) {
        if (index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(request, response, this);
    }
}

客户端调用

1
2
3
4
5
6
7
Request request = null;
Response response = null;

FilterChain filterChain = new FilterChain()
        .addFilter(new FirstFilter())
        .addFilter(new SecondFilter());
filterChain.doFilter(request, response);
皖ICP备2024056275号-1
发表了78篇文章 · 总计149.56k字
本站已稳定运行