职责链模式

简介: • 定义:为了避免请求的发送者和接收者之间的耦合关系,使多个接受对象都有机会处理请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。优点:1.客户端与具体的处理者解耦,客户端只认识一个Hanlder接口,降低了客户端(即请求发送者)与处理者的耦合度。


定义:为了避免请求的发送者和接收者之间的耦合关系,使多个接受对象都有机会处理请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

优点:1.客户端与具体的处理者解耦,客户端只认识一个Hanlder接口,降低了客户端(即请求发送者)与处理者的耦合度。

          2.简化了对象。使得对象不需要知道链的结构。

缺点:1.提高了系统的复杂度。

          2.不能保证一定被处理。          

使用场景:多个类分别多次的调用。

类图:

image.pngimage.png

通用代码:

Handler 接口

1. static abstract class Handler {
2. protected Handler mNextHandler;
3. 
4. public void setnextHanlder(Handler successor) {
5. this.mNextHandler = successor;
6.         }
7. 
8. public abstract void handleRequest(String request);
9.     }

具体的处理类

1. static class ConcreteHandlerA extends Handler {
2. 
3. @Override
4.         public void handleRequest(String request) {
5. if ("requestA".equals(request)) {
6. //doSomething
7. return;
8.             }
9. //给下一个责任类
10. if (this.mNextHandler != null) {
11. this.mNextHandler.handleRequest(request);
12.             }
13.         }
14.     }

调用

1. class Client {
2. public static void main(String[] args) {
3. Handler handlerA = new ConcreteHandlerA();
4. Handler handlerB = new ConcreteHandlerB();
5. //放入下一个责任类 
6.         handlerA.setnextHanlder(handlerB);
7.         handlerA.handleRequest("requestB");
8.     }

栗子

抽象审批类():设计的核心类。处理请求和跳转请求。

1. public abstract class ClassType{
2.  protected ClassType nextClassType;//下个责任对象
3.  public ClassType(String name) {
4.    super();
5.    this.name = name;
6.  }
7.  //设定下个责任对象
8.  public void setNextClassType(ClassType nextClassType) {
9.    this.nextClassType = nextClassType;
10.   }
11.   /**
12.    * 处理请求的核心的业务方法
13.    * @param request
14.    */
15.   public abstract void handleRequest(Long age);
16. }

抽象实现类

1. public class smallClass extends ClassType{
2. 
3. 
4.  @Override
5.  public void handleRequest(Long age) {
6.    if(age < 6){
7.      System.out.println("是小班我的人");
8.    }else{      
9.      this.nextLeader.handleRequest(age);
10.     }
11.   }
12. }
1. public class middleClass extends ClassType{
2. 
3. 
4.  @Override
5.  public void handleRequest(Long age) {
6.    if(age > 6 && age < 8){
7.      System.out.println("是中班班我的人");
8.    }else{      
9.      this.nextLeader.handleRequest(age);
10.     }
11.   }
12. }
1. public class bigClass extends ClassType{
2. 
3. 
4.  @Override
5.  public void handleRequest(Long age) {
6.    if(age > 8 && age < 10){
7.      System.out.println("是大班班我的人");
8.    }else{      
9.      System.out.println("多大了 还来幼儿园");
10.     }
11.   }
12. }

调用

1. 
2. public class Client {
3. 
4.  public static void main(String[] args) {
5.    ClassType small = new smallClass();
6.    ClassType middle = new middleClass();
7.    ClassType big = new bigClass();
8. 
9. 
10.     //组织责任链对象关系
11.     small.setNextLeader(middle);
12.     middle.setNextLeader(big);
13. 
14.     //开始请假操作
15.     small.handleRequest(12);
16.   }

 


相关文章
|
8月前
|
设计模式
行为设计模式之职责链模式
行为设计模式之职责链模式
|
设计模式 缓存 Java
行为型设计模式08-职责链模式
行为型设计模式08-职责链模式
45 0
|
9月前
|
设计模式
职责链模式
职责链模式
62 0
|
设计模式
设计模式——职责链模式
设计模式——职责链模式
|
设计模式 Java
设计模式~~~责任链模式(职责链模式)
设计模式~~~责任链模式(职责链模式)
79 0
|
设计模式
设计模式-职责链模式
设计模式-职责链模式
72 0
|
设计模式
职责链设计模式解读
职责链设计模式解读
|
设计模式 存储
设计模式之职责链模式
设计模式之职责链模式
设计模式之职责链模式
|
设计模式 Java 程序员
设计模式轻松学【二五】职责链模式
有时候,出了某件事,我们去解决,找到A,结果A踢皮球,说这不关我的事,去找B解决,然后我们就去找B,结果B也说,这跟我没关系,快去找C,就这样,我们就被踢来踢去,这就是责任链模式的思想,在找到正确的人解决之前,我们被不断的踢给一个有一个人,职责链就是告诉我们类应该有自己的职责,不属于自己职责范围的就推给其他类。
161 0
设计模式轻松学【二五】职责链模式
|
设计模式 数据处理
我学会了,职责链模式
职责链模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。
130 0
我学会了,职责链模式