美文网首页
责任链模式

责任链模式

作者: Teemo_fca4 | 来源:发表于2020-05-07 21:08 被阅读0次

    责任链模式
    传统做法就是使用if else

    //请求类
    public class PurchaseRequest {
    
        private int type = 0; //请求类型
        private float price = 0.0f; //请求金额
        private int id = 0;
        //构造器
        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;
        }
    }
    //处理类
    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);
    }
    
    //处理类实现
    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);
            }
        }
    }
    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);
            }
        }
    }
    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);
            }
        }
    }
    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);
            }
        }
    }
    
    
    public class Client {
    
        public static void main(String[] args) {
            PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1);
            
            //创建相关的审批人
            DepartmentApprover departmentApprover = new DepartmentApprover("张主任");
            CollegeApprover collegeApprover = new CollegeApprover("李院长");
            ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副校");
            SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("佟校长");
        
            //需要将各个审批级别的下一个设置好 (处理人构成环形: )  这里就是生成处理链条 感觉还不如if else 直接
            departmentApprover.setApprover(collegeApprover);
            collegeApprover.setApprover(viceSchoolMasterApprover);
            viceSchoolMasterApprover.setApprover(schoolMasterApprover);
            schoolMasterApprover.setApprover(departmentApprover);
            
            departmentApprover.processRequest(purchaseRequest);
            viceSchoolMasterApprover.processRequest(purchaseRequest);
        }
    }
    

    相关文章

      网友评论

          本文标题:责任链模式

          本文链接:https://www.haomeiwen.com/subject/yemvvctx.html