【转】Chain of Responsibility模式(职责链)

Java深入到一定程度,就不可避免的碰到设计模式这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循一定的编程模式,才能使自己的代码便于理解,易于交流,Chain of Responsibility(职责链模式)模式便是其中一个.
   Chain of Responsibility(CoR)职责链模式,CoR模式是用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合,唯一共同点是在他们之间传递request. 也就是说,来了一个请求,A类先处理,如果没有处理,就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条(chain)一样传递下去。
   优点: 因为无法预知来自外界(客户端)的请求是属于哪种类型,每个类如果碰到它不能处理的请求只要放弃就可以。
   缺点: 缺点是效率低,因为一个请求的完成可能要遍历到最后才可能完成;扩展性差,因为在CoR中,一定要有一个统一的接口Handler.局限性就在这里.
 
   在此写了10个java类来描述说明Chain of Responsibility设计模式的实现方式;

  1、 Handler.java    请求处理类接口
  2、 HandlerStudent.java  学生请求处理类
  3、 HandlerTeacher.java  老师请求处理类
  4、 HandlerSchoolmaster.java 校长请求处理类
  5、 Request.java    请求类接口
  6、 RequestStudent.java  学生请求类
  7、 RequestTeacher.java  老师请求类
  8、 RequestSchoolmaster.java 校长请求类
  9、 RequestPresident.java  总统请求类
  10、TestCoR.java    带有main方法的测试类



===============   1、 Handler.java
package cor;
public interface Handler {
  public void handleRequest(Request request);
}
===============  1 end



===============   2、 HandlerStudent.java
package cor;
public class HandlerStudent implements Handler {

  //下一个Handler对象
  private Handler nextSuccessor;

  public HandlerStudent(Handler nextSuccessor) {
    this.nextSuccessor = nextSuccessor;
  }

  public void handleRequest(Request request) {
    //处理 StudentRequest
    if(request.getType().equalsIgnoreCase("StudentRequest")){
      System.out.println("HandlerStudent begin to response StudentRequest;");
      request.execute();
    //把请求传递给其它受理者
    }else{
      if(this.nextSuccessor != null){
        nextSuccessor.handleRequest(request);
      }else{
        System.out.println("request[" + request.getType() + "] has not handler to deal!");
      }
    }
  }
}
===============   2 end



===============   3、 HandlerTeacher.java
package cor;
public class HandlerTeacher implements Handler {

  //下一个Handler对象
  private Handler nextSuccessor;

  public HandlerTeacher(Handler nextSuccessor) {
    this.nextSuccessor = nextSuccessor;
  }

  public void handleRequest(Request request) {
    //处理 TeacherRequest
    if(request.getType().equalsIgnoreCase("TeacherRequest")){
      System.out.println("HandlerTeacher begin to response StudentRequest;");
      request.execute();
    //把请求传递给其它受理者
    }else{
      if(this.nextSuccessor != null){
        nextSuccessor.handleRequest(request);
      }else{
        System.out.println("request[" + request.getType() + "] has not handler to deal!");
      }
    }
  }
}
===============   3 end



===============   4、 HandlerSchoolmaster.java
package cor;
public class HandlerSchoolmaster implements Handler {

  //下一个Handler对象
  private Handler nextSuccessor;

  public HandlerSchoolmaster(Handler nextSuccessor) {
    this.nextSuccessor = nextSuccessor;
  }

  public void handleRequest(Request request) {
    //处理 SchoolmasterRequest
    if(request.getType().equalsIgnoreCase("SchoolmasterRequest")){
      System.out.println("HandlerSchoolmaster begin to response StudentRequest;");
      request.execute();
    //把请求传递给其它受理者
    }else{
      if(this.nextSuccessor != null){
        nextSuccessor.handleRequest(request);
      }else{
        System.out.println("request[" + request.getType() + "] has not handler to deal!");
      }
    }
  }
}
===============   4 end



===============   5、 Request.java
package cor;
public interface Request {
  //请求类型
  public String getType();
  //执行任务
  public void execute();
}
===============   5 end



===============   6、 RequestStudent.java
package cor;
public class RequestStudent implements Request {

  public void execute() {
    System.out.println("[RequestStudent] :\t Students request has been accepted!");
  }

  public String getType() {
    return "StudentRequest";
  }
}
===============   6 end



===============   7、 RequestTeacher.java
package cor;
public class RequestTeacher implements Request {

  public void execute() {
    System.out.println("[RequestTeacher] :\t Teachers request has been accepted!");
  }

  public String getType() {
    return "TeacherRequest";
  }
}
===============   7 end



===============   8、 RequestSchoolmaster.java
package cor;
public class RequestSchoolmaster implements Request {

  public void execute() {
    System.out.println("[RequestSchoolmaster] :\t Schoolmasters request has been accepted!");
  }

  public String getType() {
    return "SchoolmasterRequest";
  }
}
===============   8 end



===============   9、 RequestPresident.java
package cor;
public class RequestPresident implements Request {

  public void execute() {
    System.out.println("[RequestPresident] :\t Presidents request has been accepted!");
  }

  public String getType() {
    return "PresidentRequest";
  }
}
===============   9 end



===============   10、 TestCoR.java
package cor;
public class TestCoR {
  public static void main(String[] args) {
    Handler handler = new HandlerStudent(new HandlerTeacher(new HandlerSchoolmaster(null)));
  
    Request studentReq = new RequestStudent();
    Request teacherReq = new RequestTeacher();
    Request schoolmasterReq = new RequestSchoolmaster();
    Request presidentReq = new RequestPresident();
  
    //传入student request
    handler.handleRequest(studentReq);
    System.out.println("-----------");
    //传入teacher request
    handler.handleRequest(teacherReq);
    System.out.println("-----------");
    //传入schoolmaster request
    handler.handleRequest(schoolmasterReq);
    System.out.println("-----------");
    //传入president request
    handler.handleRequest(presidentReq);
    System.out.println("-----------");
  }
}
===============   10 end

你可能感兴趣的:(java,设计模式,编程)