责任链模式

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

 

开发中常见场景

  - java中,异常机制

  - javascript语言中,事件的冒泡和捕获机制

  - Servlet开发中,过滤器的链式处理

  - Struts2中,拦截器的调用。

 

领导抽象类Leader

public abstract class Leader {
    protected String name;
    protected Leader nextLeader;
    
    public Leader() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Leader(String name, Leader nextLeader) {
        super();
        this.name = name;
        this.nextLeader = nextLeader;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Leader getNextLeader() {
        return nextLeader;
    }
    public void setNextLeader(Leader nextLeader) {
        this.nextLeader = nextLeader;
    }
    public abstract void handleLeaveRequest(LeaveRequest leave);
    
    
}
View Code

 

领导实现类:Director、Manager、GeneralManager

public class Director extends Leader{

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

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

    @Override
    public void handleLeaveRequest(LeaveRequest leave) {
        if(leave.getLeaveDays() < 3 ){
            System.out.println("主任审批通过");
        }else{
            nextLeader.handleLeaveRequest(leave);
        }
        
    }

}

public class Manager extends Leader{

    public Manager() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Manager(String name, Leader nextLeader) {
        super(name, nextLeader);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void handleLeaveRequest(LeaveRequest leave) {
        if(leave.getLeaveDays() < 10 ){
            System.out.println("经理审批通过");
        }else{
            nextLeader.handleLeaveRequest(leave);
        }
        
    }

}
public class GeneralManager extends Leader {
    
    public GeneralManager() {
        super();
        // TODO Auto-generated constructor stub
    }

    public GeneralManager(String name, Leader nextLeader) {
        super(name, nextLeader);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void handleLeaveRequest(LeaveRequest leave) {
        if(leave.getLeaveDays() < 30 ){
            System.out.println("总经理审批通过");
        }else{
            System.out.println("莫非想辞职,想请假"+leave.getLeaveDays());
        }
    }

}
View Code

 

请假条类LeaveRequest

public class LeaveRequest {
    
    private String name;
    private Integer leaveDays;
    private String leaveReason;
    
    
    
    public LeaveRequest() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
    
    public LeaveRequest(String name, Integer leaveDays, String leaveReason) {
        super();
        this.name = name;
        this.leaveDays = leaveDays;
        this.leaveReason = leaveReason;
    }



    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getLeaveDays() {
        return leaveDays;
    }
    public void setLeaveDays(Integer leaveDays) {
        this.leaveDays = leaveDays;
    }
    public String getLeaveReason() {
        return leaveReason;
    }
    public void setLeaveReason(String leaveReason) {
        this.leaveReason = leaveReason;
    }
    
}
View Code

 

测试类Main

public class Main {
    
    public static void main(String[] args) {
        LeaveRequest leave =new LeaveRequest("王五",8,"请假");
        GeneralManager general=new GeneralManager("张三",null);
        Manager manager=new Manager("李四",general);
        Director director=new Director("丁丁",manager);
        
        director.handleLeaveRequest(leave);
    }

}
View Code

 

posted @ 2018-05-31 08:38  小亮的BLOG  阅读(105)  评论(0编辑  收藏  举报