设计模式-责任链模式

136 阅读2分钟

责任链模式

引言

当我们工作想要请假时,就要向领导提交请假申请,但领导的权限有大小,可能经理最多能批2天的假期,总监最多可以批5天的假期,更多的假期可能需要总经理才可以。具体的请假需要提交给谁,还得根据请假的天数来看。

正文

责任链模式是为了避免请求发送者将多个请求处理耦合在一起,将所有请求的处理者通过前一对象记住其对下一个对象的引用从而可以形成一条链,当请求发生时,将请求沿着这条链传递,知道请求完成为止。

核心思想

在责任链模式中,开发者只需要将请求发送到责任链即可,无需关心具体的细节和传递过程,从而可以将请求的发送和处理进行解耦。

代码实现

讲解

//抽象的处理者
abstract class Handler {
    private Handler next;

    public void setNext(Handler next) {
        this.next = next;
    }

    public Handler getNext() {
        return next;
    }

    public abstract void handleRequest(String request);
}
//处理者1
public class ConcreteHandler1 extends Handler {
    @Override
    public void handleRequest(String request) {
        if (request.equals("one")) {
            System.out.println("具体处理者1处理了该请求");
        } else {
            if (getNext() != null) {
                getNext().handleRequest(request);
            } else {
                System.out.println("没有人再处理该请求了");
            }
        }
    }
}
//处理者2
public class ConcreteHandler2 extends Handler {
    @Override
    public void handleRequest(String request) {
        if (request.equals("two")) {
            System.out.println("具体处理者2处理了该请求");
        } else {
            if (getNext() != null) {
                getNext().handleRequest(request);
            } else {
                System.out.println("没有人再处理该请求了");
            }
        }
    }
}
//调用
public class Client {
    public static void main(String[] args) {
        Handler handler1 = new ConcreteHandler1();
        Handler handler2 = new ConcreteHandler2();
        handler1.setNext(handler2);
        handler1.handleRequest("two");
    }
}

实例

public abstract class Leader {
    private Leader next;
    public void setNext(Leader next){
        this.next = next;
    }

    public Leader getNext(){
        return next;
    }

    //处理假期申请
    public abstract void handleVacationApply(int vacationDays);

}
public class Manager extends Leader {
    @Override
    public void handleVacationApply(int vacationDays) {
        if (vacationDays <= 2) {
            System.out.println("不超过2天的假期被经理批准了");
        } else {
            if (getNext() != null) {
                getNext().handleVacationApply(vacationDays);
            } else {
                System.out.println("没法处理这种假期");
            }
        }
    }
}
public class ChiefManager extends Leader {
    @Override
    public void handleVacationApply(int vacationDays) {
        if (vacationDays <= 5) {
            System.out.println("不超过5天的假期被总监批准了");
        } else {
            if (getNext() != null) {
                getNext().handleVacationApply(vacationDays);
            } else {
                System.out.println("没法处理这种假期");
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Leader manager = new Manager();
        Leader chiefManager = new ChiefManager();
        manager.setNext(chiefManager);
        manager.handleVacationApply(2);
        manager.handleVacationApply(3);
        manager.handleVacationApply(6);
    }
}

输出结果

不超过2天的假期被经理批准了
不超过5天的假期被总监批准了
没法处理这种假期

总结

责任链模式适合于有多个对象可以处理一个请求,而由哪个对象来处理该请求则在请求发起时就可以确定了。

责任链模式简单来说就是当某一个请求被接收时,要么该对象可以自行处理请求,要么就将请求传递到下一个对象来进行处理。