职责链模式

《大话设计模式》阅读笔记和总结。原书是C#编写的,本人用Java实现了一遍,包括每种设计模式的UML图实现和示例代码实现。
目录:设计模式
Github地址:DesignPattern

说明

定义:职责链模式(Chain of Responsibility),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

UML图:

职责链模式
职责链模式UML图.png

代码实现:

Handler类,定义一个处理请示的接口

abstract class Handler{
    protected Handler successor;
    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }
    
    // 处理请求的抽象方法
    public abstract void HandlerRequest(int request);
}

ConcreteHandler类,具体处理者类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者

/**
 * ConcreteHandler1,请求数在0和10之间则有权处理,否则转到下一位
 */
class ConcreteHandler1 extends Handler{

    @Override
    public void HandlerRequest(int request) {
        if (request >= 0 && request < 10) {
            System.out.println("ConcreteHandler1处理请求"+request);
        }else if(successor !=null){
            successor.HandlerRequest(request);
        }
        
    }
    
}

/**
 *  ConcreteHandler2,请求数在10和20之间则有权处理,否则转到下一位
 */
class ConcreteHandler2 extends Handler{

    @Override
    public void HandlerRequest(int request) {
        if (request >= 10 && request < 20) {
            System.out.println("ConcreteHandler2处理请求"+request);
        }else if(successor !=null){
            successor.HandlerRequest(request);
        }
        
    }
    
}

/**
 *  ConcreteHandler3,请求数在20和30之间则有权处理,否则转到下一位
 */
class ConcreteHandler3 extends Handler{

    @Override
    public void HandlerRequest(int request) {
        if (request >= 20 && request < 30) {
            System.out.println("ConcreteHandler3处理请求"+request);
        }else if(successor !=null){
            successor.HandlerRequest(request);
        }
        
    }
    
}

客户端代码

public class ChainOfResponsibilityPattern {
    
    public static void main(String[] args) {
        Handler h1 = new ConcreteHandler1();
        Handler h2 = new ConcreteHandler2();
        Handler h3 = new ConcreteHandler3();
        
        h1.setSuccessor(h2);
        h2.setSuccessor(h3);
        
        int[] requests = {2,5,14,22,18,3,27,20};
        for (int i = 0; i < requests.length; i++) {
            h1.HandlerRequest(requests[i]);
        }
    }

}

运行结果

ConcreteHandler1处理请求2
ConcreteHandler1处理请求5
ConcreteHandler2处理请求14
ConcreteHandler3处理请求22
ConcreteHandler2处理请求18
ConcreteHandler1处理请求3
ConcreteHandler3处理请求27
ConcreteHandler3处理请求20

示例

例子:在公司无论办什么事情都要走流程,一级一级审批。如果我们想请假,两天以内部门经理就可以批准,如果超过两天,五天以内总监有权限批准,五天以上就需要总经理批准了。如果我们想加薪就只能总经理审批。用程序模拟这个过程。

UML图:

职责链模式
职责链模式示例UML图.png

代码实现:

申请

public class Request {
    // 申请类别
    private String requestType;
    // 申请内容
    private String requestContent;
    // 申请数量
    private int number;
    public String getRequestType() {
        return requestType;
    }
    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }
    public String getRequestContent() {
        return requestContent;
    }
    public void setRequestContent(String requestContent) {
        this.requestContent = requestContent;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    
}

管理者

public abstract class Manager {

    protected String name;
    //管理者的上级
    protected Manager superior;
    public Manager(String name){
        this.name = name;
    }
    
    public void setSuperior(Manager superior) {
        this.superior = superior;
    }
    
    public abstract void RequestApplicatios(Request request);
}

总经理类

public class CommonManager extends Manager{

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

    @Override
    public void RequestApplicatios(Request request) {
        // 经理的权限就是可准许下两天以内的假期
        if(request.getRequestType().equals("请假") && request.getNumber()<=2){
            System.out.println(name+":"+request.getRequestContent()+" 数量"+request.getNumber()+"被批准");
        }
        // 其余的申请都需转到上级
        else{
            if(superior!=null){
                superior.RequestApplicatios(request);
            }
        }
    }

}

总监类

public class Majordomo extends Manager{

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

    @Override
    public void RequestApplicatios(Request request) {
        // 经理的权限就是可准许下属一周以内的假期
        if(request.getRequestType().equals("请假") && request.getNumber()<=5){
            System.out.println(name+":"+request.getRequestContent()+" 数量"+request.getNumber()+"被批准");
        }
        // 其余的申请都需转到上级
        else{
            if(superior!=null){
                superior.RequestApplicatios(request);
            }
        }
    }

}

总经理类,可以处理全部任何事情

public class GeneralManager extends Manager{

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

    @Override
    public void RequestApplicatios(Request request) {
        if(request.getRequestType().equals("请假")){
            System.out.println(name+":"+request.getRequestContent()+" 数量"+request.getNumber()+"被批准");
        }
        else if(request.getRequestType().equals("加薪") && request.getNumber() <= 500){
            System.out.println(name+":"+request.getRequestContent()+" 数量"+request.getNumber()+"被批准");
        }
        else if(request.getRequestType().equals("加薪") && request.getNumber() > 500){
            System.out.println(name+":"+request.getRequestContent()+" 数量"+request.getNumber()+"再说吧");
        }
    }

}

客户端代码

public class Main {
    
    public static void main(String[] args) {
        CommonManager jingli = new CommonManager("经理");
        Majordomo zongjian = new Majordomo("总监");
        GeneralManager zongjingli = new GeneralManager("总经理");
        jingli.setSuperior(zongjian);
        zongjian.setSuperior(zongjingli);
        
        Request request = new Request();
        request.setRequestType("请假");
        request.setRequestContent("小菜请假");
        request.setNumber(1);
        jingli.RequestApplicatios(request);
        
        Request request2 = new Request();
        request2.setRequestType("请假");
        request2.setRequestContent("小菜请假");
        request2.setNumber(4);
        jingli.RequestApplicatios(request2);
        
        Request request3 = new Request();
        request3.setRequestType("加薪");
        request3.setRequestContent("小菜请求加薪");
        request3.setNumber(500);
        jingli.RequestApplicatios(request3);
        
        Request request4 = new Request();
        request4.setRequestType("加薪");
        request4.setRequestContent("小菜请求加薪");
        request4.setNumber(1000);
        jingli.RequestApplicatios(request4);
    }

}

运行结果

经理:小菜请假 数量1被批准
总监:小菜请假 数量4被批准
总经理:小菜请求加薪 数量500被批准
总经理:小菜请求加薪 数量1000再说吧
上一篇:命令模式


下一篇:利用Google VR教你打造全景图片展示