首页 > 代码库 > 职责链模式

职责链模式

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

职责链的好处:客户提交一个请求时,请求是沿着链传递直至有一个职责对象负责处理它。

请求不用管那个对象来处理,反正最后该请求就是会被处理。这就使得接受者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需保持它所有的候选接受者的引用。

在客户端定义链的结构,也就是说,可以随时地增加或修改处理一个请求的结构。增强了给对象指派职责的灵活性。

但是,一个请求极有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理,这就很糟糕,需要事先考虑全面。

可解决原来大量的分支判断造成难维护、灵活性差的问题。

Request类

package chain_of_responsibility;

public class Request {
    private String requstType;
    private String requestContent;
    private int requestNumber;
    private String requestName;
    public String getRequstType() {
        return requstType;
    }
    public void setRequstType(String requstType) {
        this.requstType = requstType;
    }
    public String getRequestContent() {
        return requestContent;
    }
    public void setRequestContent(String requestContent) {
        this.requestContent = requestContent;
    }
    public int getRequestNumber() {
        return requestNumber;
    }
    public void setRequestNumber(int requestNumber) {
        this.requestNumber = requestNumber;
    }
    public String getRequestName() {
        return requestName;
    }
    public void setRequestName(String requestName) {
        this.requestName = requestName;
    }
    
}

Manager类

package chain_of_responsibility;

public abstract class Manager {
    private String name;
    protected Manager supperManager;
    
    public void setManager(Manager supperManager){
        this.supperManager = supperManager;
    }
    
    public Manager(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public abstract void requestApply(Request req);
}

JLManager类

package chain_of_responsibility;

public class JLManager extends Manager {

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

    @Override
    public void requestApply(Request req) {
        if("请假".equals(req.getRequstType())&&req.getRequestNumber()<3){
            System.out.println("请假人:"+req.getRequestName()+",请假内容:"+req.getRequestContent()+"请假天数:"+req.getRequestNumber()+"。经理批准了");
        }else{
            if(supperManager!=null){
                supperManager.requestApply(req);
            }
        }
    }
}

ZJManager类

package chain_of_responsibility;

public class ZJManager extends Manager {

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

    @Override
    public void requestApply(Request req) {
        if("请假".equals(req.getRequstType())){
            System.out.println("请假人:"+req.getRequestName()+",请假内容:"+req.getRequestContent()+"请假天数:"+req.getRequestNumber()+"。总监批准了");
        }else if("加薪".equals(req.getRequstType())&&req.getRequestNumber()<=500){
            System.out.println("申请加薪人:"+req.getRequestName()+",加薪说明:"+req.getRequestContent()+"加薪数:"+req.getRequestNumber()+"。总监批准了");
        }else{
            if(supperManager!=null){
                supperManager.requestApply(req);
            }
        }
    }
}

ZJLManager类

package chain_of_responsibility;

public class ZJLManager extends Manager {

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

    @Override
    public void requestApply(Request req) {
        if("请假".equals(req.getRequstType())){
            System.out.println("请假人:"+req.getRequestName()+",请假内容:"+req.getRequestContent()+"请假天数:"+req.getRequestNumber()+"。总经理批准了");
        }else if("加薪".equals(req.getRequstType())&&req.getRequestNumber()<=2000){
            System.out.println("申请加薪人:"+req.getRequestName()+",加薪说明:"+req.getRequestContent()+"加薪数:"+req.getRequestNumber()+"。总经理批准了");
        }else{
            System.out.println("要求过了,驳回申请");
        }
    }
}

Business类

package chain_of_responsibility;

public class Business {

    public static void main(String[] args) {
        Request req = new Request();
        req.setRequstType("请假");
        req.setRequestContent("家里有事");
        req.setRequestNumber(2);
        req.setRequestName("新人");
        Manager jl = new JLManager("经理");
        Manager zj = new ZJManager("总监");
        Manager zjl = new ZJLManager("总经理");
        jl.setManager(zj);
        zj.setManager(zjl);
        jl.requestApply(req);
        
        Request req1 = new Request();
        req1.setRequstType("请假");
        req1.setRequestContent("家里有事");
        req1.setRequestNumber(5);
        req1.setRequestName("新人");
        jl.requestApply(req1);
        
        Request req2 = new Request();
        req2.setRequstType("加薪");
        req2.setRequestContent("表现优秀");
        req2.setRequestNumber(1000);
        req2.setRequestName("新人");
        jl.requestApply(req2);
        
        Request req3 = new Request();
        req3.setRequstType("加薪");
        req3.setRequestContent("表现特好");
        req3.setRequestNumber(2500);
        req3.setRequestName("新人");
        jl.requestApply(req3);
    }

}

 

职责链模式