OkHttp中的责任链模式的缩略版

本文介绍了一个基于责任链设计模式的请假审批系统,详细展示了从组长到大老板的审批流程,不同层级的管理者有不同的审批权限,当请假天数超过当前层级的权限时,请求会被传递给更高一级的管理者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

exclude patterns:
	开始申请休假3天。
	Mia组长开始审批,休假3天
	休假3天申请结果:	Mia组长已批准你的3天休假申请。

	开始申请休假6天。
	Mia组长开始审批,休假6天
	6天休假申请超出Mia组长的审批权限
	Morgen项目经理开始审批,休假6天
	休假6天申请结果:	Morgen项目经理已批准你的6天休假申请。

	开始申请休假21天。
	Mia组长开始审批,休假21天
	21天休假申请超出Mia组长的审批权限
	Morgen项目经理开始审批,休假21天
	21天休假申请超出Morgen项目经理的审批权限
	Andrea总监开始审批,休假21天
	休假21天申请结果:	Andrea总监已批准你的21天休假申请。

	开始申请休假32天。
	Mia组长开始审批,休假32天
	32天休假申请超出Mia组长的审批权限
	Morgen项目经理开始审批,休假32天
	32天休假申请超出Morgen项目经理的审批权限
	Andrea总监开始审批,休假32天
	32天休假申请超出Andrea总监的审批权限
	Cyra副总裁开始审批,休假32天
	休假32天申请结果:	Cyra副总裁已批准你的32天休假申请。

	开始申请休假108天。
	Mia组长开始审批,休假108天
	108天休假申请超出Mia组长的审批权限
	Morgen项目经理开始审批,休假108天
	108天休假申请超出Morgen项目经理的审批权限
	Andrea总监开始审批,休假108天
	108天休假申请超出Andrea总监的审批权限
	Cyra副总裁开始审批,休假108天
	108天休假申请超出Cyra副总裁的审批权限
	Iris大老板开始审批,休假108天
	休假108天申请结果:	Iris老板已欣然同意你的108天休假申请,并慷慨特批无限期延长假期,工资同理!

	开始申请休假367天。
	Mia组长开始审批,休假367天
	367天休假申请超出Mia组长的审批权限
	Morgen项目经理开始审批,休假367天
	367天休假申请超出Morgen项目经理的审批权限
	Andrea总监开始审批,休假367天
	367天休假申请超出Andrea总监的审批权限
	Cyra副总裁开始审批,休假367天
	367天休假申请超出Cyra副总裁的审批权限
	Iris大老板开始审批,休假367天
	index=5, interceptors.size=5
	休假367天申请结果:	抱歉!没有这样的老板能批的了你367的假期,请尝试其它BOSS......

Class transformation time: 0.016466353s for 116 classes or 1.4195131896551724E-4s per class

Process finished with exit code 0

 

package com.windfallsheng.myresponsibilitychain;

import java.util.ArrayList;
import java.util.List;

class Client {

    public static void main(String[] args) {

        Client client = new Client();

        final List<Interceptor> interceptors = client.initInterceptor();

        System.out.println("\t" + "开始申请休假" + 3 + "天。");
        String day3 = client.getInterceptorChain(interceptors, 3);
        System.out.println("\t" + "休假3天申请结果:" + day3 + "\n");

        System.out.println("\t" + "开始申请休假" + 6 + "天。");
        String day6 = client.getInterceptorChain(interceptors, 6);
        System.out.println("\t" + "休假6天申请结果:" + day6 + "\n");

        System.out.println("\t" + "开始申请休假" + 21 + "天。");
        String day21 = client.getInterceptorChain(interceptors, 21);
        System.out.println("\t" + "休假21天申请结果:" + day21 + "\n");

        System.out.println("\t" + "开始申请休假" + 32 + "天。");
        String day32 = client.getInterceptorChain(interceptors, 32);
        System.out.println("\t" + "休假32天申请结果:" + day32 + "\n");

        System.out.println("\t" + "开始申请休假" + 108 + "天。");
        String day108 = client.getInterceptorChain(interceptors, 108);
        System.out.println("\t" + "休假108天申请结果:" + day108 + "\n");

        System.out.println("\t" + "开始申请休假" + 367 + "天。");
        String day367 = client.getInterceptorChain(interceptors, 367);
        System.out.println("\t" + "休假367天申请结果:" + day367 + "\n");
    }

    private List<Interceptor> initInterceptor() {
        List<Interceptor> interceptors = new ArrayList<>();
        interceptors.add(new GroupLeaderInterceptor("Mia"));
        interceptors.add(new ProjectManagerInterceptor("Morgen"));
        interceptors.add(new DirectorInterceptor("Andrea"));
        interceptors.add(new VicePresidentInterceptor("Cyra"));
        interceptors.add(new BossInterceptor("Iris"));
        return interceptors;
    }

    private String getInterceptorChain(List<Interceptor> interceptors, int days) {
        Interceptor.Chain chain = new RealInterceptorChain(
                interceptors, 0, days);
        return chain.proceed(days);
    }
}
package com.windfallsheng.myresponsibilitychain;

interface Interceptor {

    String intercept(Chain chain);

    interface Chain {

        String proceed(int days);

    }
}
package com.windfallsheng.myresponsibilitychain;

import java.util.List;

class RealInterceptorChain implements Interceptor.Chain {

    private final List<Interceptor> interceptors;
    private final int index;
    /**
     * 休假天数
     */
    private final int days;

    public RealInterceptorChain(List<Interceptor> interceptors, int index, int days) {
        this.interceptors = interceptors;
        this.index = index;
        this.days = days;
    }

    @Override
    public String proceed(int days) {
        if (index >= interceptors.size()) {
            System.out.println("\t" + "index=" + index + ", interceptors.size=" + interceptors.size());
            return "\t" + "抱歉!没有这样的老板能批的了你" + days + "的假期,请尝试其它BOSS......";
        }
        RealInterceptorChain next = new RealInterceptorChain(
                interceptors, index + 1, days);
        Interceptor interceptor = interceptors.get(index);
        String result = interceptor.intercept(next);
        return result;
    }

    public int request() {
        return days;
    }
}

 

package com.windfallsheng.myresponsibilitychain;

/**
 * 组长
 */
class GroupLeaderInterceptor implements Interceptor {

    private String name;

    public GroupLeaderInterceptor(String name) {
        this.name = name;
    }

    @Override
    public String intercept(Chain chain) {
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        int days = realChain.request();
        System.out.println("\t" + name + "组长开始审批,休假" + days + "天");
        if (days > 3) {
            System.out.println("\t" + days + "天休假申请超出" + name + "组长的审批权限");
        } else {
            return "\t" + name + "组长已批准你的" + days + "天休假申请。";
        }
        return realChain.proceed(days);
    }
}
package com.windfallsheng.myresponsibilitychain;

/**
 * 项目经理
 */
class ProjectManagerInterceptor implements Interceptor {

    private String name;

    public ProjectManagerInterceptor(String name) {
        this.name = name;
    }

    @Override
    public String intercept(Chain chain) {
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        int days = realChain.request();
        System.out.println("\t" + name + "项目经理开始审批,休假" + days + "天");
        if (days > 7) {
            System.out.println("\t" + days + "天休假申请超出" + name + "项目经理的审批权限");
        } else {
            return "\t" + name + "项目经理已批准你的" + days + "天休假申请。";
        }
        return realChain.proceed(days);
    }
}
package com.windfallsheng.myresponsibilitychain;

/**
 * 总监
 */
class DirectorInterceptor implements Interceptor {

    private String name;

    public DirectorInterceptor(String name) {
        this.name = name;
    }

    @Override
    public String intercept(Chain chain) {
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        int days = realChain.request();
        System.out.println("\t" + name + "总监开始审批,休假" + days + "天");
        if (days > 30) {
            System.out.println("\t" + days + "天休假申请超出" + name + "总监的审批权限");
        } else {
            return "\t" + name + "总监已批准你的" + days + "天休假申请。";
        }
        return realChain.proceed(days);
    }
}
package com.windfallsheng.myresponsibilitychain;

/**
 * 副总裁
 */
class VicePresidentInterceptor implements Interceptor {

    private String name;

    public VicePresidentInterceptor(String name) {
        this.name = name;
    }

    @Override
    public String intercept(Chain chain) {
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        int days = realChain.request();
        System.out.println("\t" + name + "副总裁开始审批,休假" + days + "天");
        if (days > 90) {
            System.out.println("\t" + days + "天休假申请超出" + name + "副总裁的审批权限");
        } else {
            return "\t" + name + "副总裁已批准你的" + days + "天休假申请。";
        }
        return realChain.proceed(days);
    }
}
package com.windfallsheng.myresponsibilitychain;

/**
 * 大老板
 */
class BossInterceptor implements Interceptor {

    private String name;

    public BossInterceptor(String name) {
        this.name = name;
    }

    @Override
    public String intercept(Chain chain) {
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        int days = realChain.request();
        System.out.println("\t" + name + "大老板开始审批,休假" + days + "天");
        if (days >= 90 && days < 365) {
            return "\t" + name + "老板已欣然同意你的" + days + "天休假申请,并慷慨特批无限期延长假期,工资同理!";
        }
        return realChain.proceed(days);
    }
}

 

源码下载地址:优快云

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

windfallsheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值