Java设计模式之职责链模式

1. 职责链模式概述

1.1 职责链简介

1.职责链模式(Chain of Responsibility Pattern), 又叫 责任链模式,为请求创建了一个接收者对象的链(简单示意 图).这种模式对请求的发送者和接收者进行解耦。
2.职责链模式通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的 请求传给下一个接收者,依此类推。
3.这种类型的设计模式属于行为型模式

1.2 职责链类图

在这里插入图片描述

1.3 职责链注意事项

1.将请求和处理分开,实现解耦,提高系统的灵活性。
2.简化了对象,使对象不需要知道链的结构。
3.性能会受到影响,特别是在链比较长的时候,因此需控制链中最大节点数量,一般通过在 Handler 中设置一个 最大节点数量,在 setNext()方法中判断是否已经超过阀值,超过则不允许该链建立,避免出现超长链无意识地 破坏系统性能。
4.调试不方便.采用了类似递归的方式,调试时逻辑可能比较复杂 5) 最佳应用场景:有多个对象可以处理同一个请求时,比如:多级请求,请假/加薪等审批流程,Java Web 中 Tomcat 对 Encoding 的处理,拦截器。

2. 职责链模式实现

2.1 项目说明

Approver:审批人抽象类
DepartmentApprover:部门审批
CollegeApprover:院长审批
ViceSchoolMasterApprover:副校长审批
SchoolMasterApprover:校长审批

PurchaseRequest:购买请求类

Client:职责链模式测试类

2.2 项目实现

Approver:审批人抽象类

package com.zrj.design.responsibility;

/**
 * 审批人抽象类
 *
 * @author zrj
 * @since 2021/11/8
 **/
public abstract class Approver {
    Approver approver;  //下一个处理者
    String name; // 名字

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

    //下一个处理者
    public void setApprover(Approver approver) {
        this.approver = approver;
    }

    //处理审批请求的方法,得到一个请求, 处理是子类完成,因此该方法做成抽象
    public abstract void processRequest(PurchaseRequest purchaseRequest);
}

DepartmentApprover:部门审批

package com.zrj.design.responsibility;

/**
 * 部门审批
 * @author zrj
 * @since 2021/11/8
 **/
public class DepartmentApprover extends Approver {
    public DepartmentApprover(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() <= 5000) {
            System.out.println("请求编号id=" + purchaseRequest.getId() + "," + this.name + "已处理!");
        } else {
            approver.processRequest(purchaseRequest);
        }
    }

}

CollegeApprover:院长审批

package com.zrj.design.responsibility;

/**
 * 院长审批
 *
 * @author zrj
 * @since 2021/11/8
 **/
public class CollegeApprover extends Approver {

    public CollegeApprover(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000) {
            System.out.println("请求编号id=" + purchaseRequest.getId() + "," + this.name + "已处理!");
        } else {
            approver.processRequest(purchaseRequest);
        }
    }
}



ViceSchoolMasterApprover:副校长审批

package com.zrj.design.responsibility;

/**
 * 副校长审批
 *
 * @author zrj
 * @since 2021/11/8
 **/
public class ViceSchoolMasterApprover extends Approver {
    public ViceSchoolMasterApprover(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() < 10000 && purchaseRequest.getPrice() <= 30000) {
            System.out.println("请求编号id=" + purchaseRequest.getId() + "," + this.name + "已处理!");
        } else {
            approver.processRequest(purchaseRequest);
        }
    }
}

SchoolMasterApprover:校长审批

package com.zrj.design.responsibility;

/**
 * 校长审批
 *
 * @author zrj
 * @since 2021/11/8
 **/
public class SchoolMasterApprover extends Approver {
    public SchoolMasterApprover(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() > 30000) {
            System.out.println("请求编号id=" + purchaseRequest.getId() + "," + this.name + "已处理!");
        } else {
            approver.processRequest(purchaseRequest);
        }
    }
}


PurchaseRequest:购买请求类

package com.zrj.design.responsibility;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 购买请求类
 *
 * @author zrj
 * @since 2021/11/8
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PurchaseRequest {
    //编号
    private int id = 0;
    //请求类型
    private int type = 0;
    //请求金额
    private float price = 0.0f;
}


Client:职责链模式测试类

package com.zrj.design.responsibility;

/**
 * 职责链模式测试类
 *
 * @author zrj
 * @since 2021/11/8
 **/
public class Client {
    public static void main(String[] args) {
        //创建一个请求
        PurchaseRequest purchaseRequest = new PurchaseRequest(1, 1, 31000);

        //创建相关的审批人
        DepartmentApprover departmentApprover = new DepartmentApprover("张主任");
        CollegeApprover collegeApprover = new CollegeApprover("李院长");
        ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("刘副校");
        SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("王校长");

        //需要将各个审批级别的下一个设置好 (处理人构成环形: )
        departmentApprover.setApprover(collegeApprover);
        collegeApprover.setApprover(viceSchoolMasterApprover);
        viceSchoolMasterApprover.setApprover(schoolMasterApprover);
        schoolMasterApprover.setApprover(departmentApprover);

        departmentApprover.processRequest(purchaseRequest);
        //viceSchoolMasterApprover.processRequest(purchaseRequest);
    }

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值