设计模式-责任链模式(十四)

  • 责任链模式
    每一个对象都有其下家的引用,这样就可以形成一条链
    请求在链上传递,客户端不知道哪一个对象会处理这个请求,这就可以让客户端在不知道的情况下重新分配责任.

在处理HTTP 请求的框架里面可以用该模式.
实例:击鼓传花

责任链模式可以一个对象只处理本人关注的,省得写一堆 if else if,我们只写一个if else
形如:

if (我想处理) {
  // 处理之 
  this.execute();
}else{
    //扔给下一个
    successor.handle();
}
  • 上类图:
设计模式-责任链模式(十四)_第1张图片
image.png
  • 示例代码:
  1. 先定义一个链接口,里面要有个 handleChain 处理链的方法
package com.byedbl.chain;

/**
 * The interface of the chain
 * You can use AddChain function to modify the chain dynamically
 */
public interface Chain {
    void addChain(Chain c);

    void handleChain(String mesg);

    Chain getChain();
}
  1. 定义一堆实现者,这里以经典的代码管理为例,有manager,PM,Programmer,QA,Others角色
package com.byedbl.chain;
/**
 *  A beginner of the chain
 *  The resposibility of manager is to get a project
 */
public class Manager implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Get Project";
    ;

    public Manager() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A manager  -->  Get a Project");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}
package com.byedbl.chain;
/**
 *  A member of the chain
 *  The resposibility of PM is to design the project
 */
public class ProjectManager implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Design";

    public ProjectManager() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A PM  -->  Design");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}
package com.byedbl.chain;
/**
 *  A member of the chain
 *  The resposibility of Programmer is coding
 */
public class Programmer implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Coding";

    public Programmer() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A Programmer  -->  Coding");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}

package com.byedbl.chain;
/**
 *  A member of the chain
 *  The resposibility of QA is test
 */
public class QA implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Test";

    public QA() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A QA  -->  Test");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}
package com.byedbl.chain;
/**
 *  The end of the chain
 *  The resposibility of Others is handle exeception 
 */
public class Others implements Chain {
    private Chain nextChain = null;
    private String responsibility = "";
    
    public Others() {
    }
    public void addChain(Chain c) {
        nextChain = c;
    }
    
    public Chain getChain() {
        return nextChain;
    }
    
    public void handleChain(String mesg) {
            System.out.println("No one can handle -->  " + mesg);
    }
    
}
  1. 客户端代码
package com.byedbl.chain;
/**
 *  A client to test
 */
public class Test  {
    public static void main(String[] args) {
        Manager aManager = new Manager();
        ProjectManager aPM = new ProjectManager();
        Programmer aProgrammer = new Programmer();
        QA aQA = new QA();
        Others others = new Others();

        aManager.addChain(aPM);
        aPM.addChain(aProgrammer);
        aProgrammer.addChain(aQA);
        aQA.addChain(others);

        aManager.handleChain("Get Project");
        aManager.handleChain("Design");
        aManager.handleChain("Coding");
        aManager.handleChain("Test");
        aManager.handleChain("Kill La Deng !");
    }
}

你可能感兴趣的:(设计模式-责任链模式(十四))