自学内容网 自学内容网

设计模式-职责链模式

就是一个链表,指向下一个处理请求的节点,在节点内进行判断
在这里插入图片描述
设计类图:
在这里插入图片描述
一个抽象的处理父类

package com.ChainOfResponsibility;

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 processPurchaseRequest(PurchaseRequest purchaseRequest);
}

4个具体的处理子类:

package com.ChainOfResponsibility;

public class DepartmentApprover extends Approver {

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

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

    }
}

package com.ChainOfResponsibility;

public class CollegeApprover extends Approver {

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

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

    }
}

package com.ChainOfResponsibility;

public class ViceSchoolMasterApprover extends Approver {

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

    
    public void processPurchaseRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() >= 10000 && purchaseRequest.getPrice() <=30000){
            System.out.println("请求编号id=" + purchaseRequest.getId() + "被" +this.name +"处理");
        }else {
            approver.processPurchaseRequest(purchaseRequest);
        }

    }
}

package com.ChainOfResponsibility;

public class SchoolMasterApprover extends Approver {

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


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

    }
}

购买请求类

package com.ChainOfResponsibility;

public class PurchaseRequest {

    private int type;
    private float price;
    private int id;

    public PurchaseRequest(int type, float price, int id) {
        this.type = type;
        this.price = price;
        this.id = id;
    }

    public int getType() {
        return type;
    }

    public float getPrice() {
        return price;
    }

    public int getId() {
        return id;
    }
}

客户端请求:

package com.ChainOfResponsibility;

public class Client {
    public static void main(String[] args) {
        //创建一个请求
        PurchaseRequest purchaseRequest = new PurchaseRequest(1, 30001, 1);
        //创建相关审批人
        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);

        departmentApprover.processPurchaseRequest(purchaseRequest);
        viceSchoolMasterApprover.processPurchaseRequest(purchaseRequest);

    }
}


原文地址:https://blog.csdn.net/weixin_45332508/article/details/137902869

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!