设计模式学习(十二) 责任链模式

定义:

  -- 将能够处理同一类请求的对象连成一条链,所提交的请求沿着链传递,链上的对象逐个判断是否有能力处理该请求,如果能则处理,如果不能则传递给链上的下一个对象

场景:

  -- 打牌时,轮流出牌

  -- 接力赛跑

  -- 大学中,奖学金审批

以公司中请假条的审批为例:

代码:

package com.lp.chaiOfResp;

//封装请假的基本信息
public class LeaveRequest {

    private String empName;
    private int leaveDays;
    private String reason;
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public int getLeaveDays() {
        return leaveDays;
    }
    public void setLeaveDays(int leaveDays) {
        this.leaveDays = leaveDays;
    }
    public String getReason() {
        return reason;
    }
    public void setReason(String reason) {
        this.reason = reason;
    }
    
    public LeaveRequest(String empName, int leaveDays, String reason) {
        super();
        this.empName = empName;
        this.leaveDays = leaveDays;
        this.reason = reason;
    }
    
}
请假条信息

 

package com.lp.chaiOfResp;

public abstract class Leader {
    
    protected String name;
    protected Leader nestLeader; //责任链上的后记对象

    public Leader(String name) {
        super();
        this.name = name;
    }

    public void setNestLeader(Leader nestLeader) {
        this.nestLeader = nestLeader;
    }
    
    //处理请求的抽象方法
    public  abstract void handleRequest(LeaveRequest request);
}
领导人员抽象类

 

package com.lp.chaiOfResp;

public class Director  extends Leader {

    public Director(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void handleRequest(LeaveRequest request) {

        if(request.getLeaveDays()<3){
            System.out.println("员工:"+ request.getEmpName() + "请假天数:" + request.getLeaveDays() + "理由:" + request.getReason() );
            System.out.println("主任:" + this.name + "审核通过");
        }else{
            if(this.nestLeader!=null){
                this.nestLeader.handleRequest(request);
            }
        }
        
    }
    
    
    
}
主任

 

package com.lp.chaiOfResp;

public class Manager extends Leader {

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

    @Override
    public void handleRequest(LeaveRequest request) {

        if(request.getLeaveDays()<10){
            System.out.println("员工:"+ request.getEmpName() + "请假天数:" + request.getLeaveDays() + "理由:" + request.getReason() );
            System.out.println("经理:" + this.name + "审核通过");
        }else{
            if(this.nestLeader!=null){
                this.nestLeader.handleRequest(request);
            }
        }
        
    }
    
    
    
}
经理

 

package com.lp.chaiOfResp;

public class GeneralManager extends Leader {

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

    @Override
    public void handleRequest(LeaveRequest request) {

        if(request.getLeaveDays()<30){
            System.out.println("员工:" + request.getEmpName() + "请假天数:" + request.getLeaveDays() + "理由:" + request.getReason() );
            System.out.println("总经理:" + this.name + "审核通过");
        }else{
            System.out.println("莫非" + request.getEmpName() + "想辞职? 居然请假 " + request.getLeaveDays() + "天" );
            }
        }
        
}
    
总经理

 

package com.lp.chaiOfResp;

public class Client {
    public static void main(String[] args){
        Leader a = new Director("张主任");
        Leader b = new Manager("李经理");
        Leader c = new GeneralManager("王总经理");
        
        //组织责任链对象的关系
        a.setNestLeader(b);
        b.setNestLeader(c);
        
        //开始请假操作
        LeaveRequest req1 = new LeaveRequest("李鹏", 9, "回老家");
        a.handleRequest(req1);
                
    
    }
}
客户端

 

posted @ 2015-04-19 23:01  李_鹏  阅读(147)  评论(0编辑  收藏  举报