zoukankan      html  css  js  c++  java
  • 5 行为型模式之

    责任链模式介绍:

    责任链模式是行为模式之一,什么是“链”?我们将多个节点首尾相连,就形成了链,对于链式结构,每个节点都可以拆开再重新连接,因此链式结构也具有很好的灵活性。将这样一种结构应用于编程领域,将每一个节点看作是一个对象,每一个对象拥有不同的处理逻辑,将一个请求从一个链的首端发出,沿着链的路径依次传递给每一个节点对象,直至有对象处理这个请求为止,我们将这样的一种模式称为责任链模式。

    责任链模式的定义:

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

    使用场景:多个对象可以处理同一请求,但具体由哪个对象处理则在运行时动态决定。

    在请求处理者不明确的情况下向多个对象中的一个提交一个请求

    例子:我们以工作中报销费用为例子,一般我们先找组长报销,组长的权限是一定的,只能报一定范围内的钱,如果走出了组长的权限,我们就要向经理报,如果经理也报不了,就向总监报销,如果连总监也报不了,就直接发给老板报销,如果老板也报不了那就真的报不了啦。但是如果中间任何一个报销了,就返回,不再往上申请了。

    代码如下:

     1 /**
     2  *
     3  * 使多个对象都有计划处理请求,从而避免请求的发送者和接受者之间的耦合关系。
     4  * 将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
     5  *
     6  */
     7 public abstract class Leader {
     8     protected Leader preLeader;   //上一级的领导
     9 
    10     public final void handleRequest(int money){
    11         if(handler(money)){
    12             return;
    13         }
    14 
    15         if(preLeader != null){
    16             preLeader.handleRequest(money);
    17         }
    18     }
    19 
    20     public void setPreLeader(Leader leader){
    21         this.preLeader = leader;
    22     }
    23 
    24     //自已能批复的最大的报销额度
    25     protected abstract int limit();
    26 
    27     //处理报销行为
    28     protected abstract boolean handler(int money);
    29 
    30 }

    组长:

     1 /**
     2  * 组长
     3  * 只多只能报1000元以内的
     4  */
     5 public class GroupLeader extends Leader{
     6     @Override
     7     protected int limit() {
     8         return 1000;
     9     }
    10 
    11     @Override
    12     protected boolean handler(int money) {
    13         if(money > limit()){
    14             return false;
    15         }
    16 
    17         System.out.println("组长批准了报销");
    18         return true;
    19     }
    20 }

    主管:

     1 /**
     2  * 主管
     3  */
     4 public class Director extends Leader{
     5     @Override
     6     protected int limit() {
     7         return 5000;
     8     }
     9 
    10     @Override
    11     protected boolean handler(int money) {
    12         if(money > limit()){
    13             return false;
    14         }
    15 
    16         System.out.println("主管批准了报销");
    17         return true;
    18     }
    19 }

    经理

     1 /**
     2  * 经理 
     3  * 最多可以报 10000
     4  */
     5 public class Manager extends Leader{
     6     @Override
     7     protected int limit() {
     8         return 10000;
     9     }
    10 
    11     @Override
    12     protected boolean handler(int money) {
    13         if(money > limit()){
    14             return false;
    15         }
    16 
    17         System.out.println("经理批准了报销");
    18         return true;
    19     }
    20 }

    老板:不受限制

     1 /**
     2  * 老板
     3  * 不限制
     4  */
     5 public class Boss extends Leader{
     6 
     7     @Override
     8     protected int limit() {
     9         return Integer.MAX_VALUE;
    10     }
    11 
    12     @Override
    13     protected boolean handler(int money) {
    14         if(money > limit()){
    15             return false;
    16         }
    17 
    18         System.out.println("老板批准了报销");
    19         return true;
    20     }
    21 }

    测试类如下:

     1 /**
     2  * 责任链模式,有两种情况:
     3  * 1 先自己处理,自己处理不了,再交给上级处理
     4  * 2 先交给上级处理,上级处理不了,再自己处理(比如类的加载机制)
     5  */
     6 public class ChainTest {
     7 
     8     public static void main(String[] args){
     9         Leader groupLeader = new GroupLeader();
    10         Leader directorLeader = new Director();
    11         Leader boss = new Boss();
    12 
    13         groupLeader.setPreLeader(directorLeader);
    14         directorLeader.setPreLeader(boss);
    15         boss.setPreLeader(null);
    16 
    17         groupLeader.handleRequest(50000);
    18 
    19     }
    20 
    21 
    22 }

    上的例子报销请求一级一级的往上传,直到有一个对象请求为止

    注:设计模式交流学习群 632945466  欢迎所有热爱技术的大牛,小菜,一起学习讨论进步提高,欢迎随时批评指正

  • 相关阅读:
    kettle excel input 利用通配符一次读入多份文件
    PowerDesigner Name、Code 映射设置
    PowerDesigner 建立约束
    PowerDesigner 创建概念模型、转换显示风格、概念模型转逻辑模型
    SQL 语言分类
    PowerDesigner 使用域、逻辑模型转物理模型、查看DDL语句
    DB、ETL、DW、OLAP、DM、BI关系结构图
    读懂BI商业智能与大数据应用的区别
    java 生成excel
    IntelliJ IDEA 2016 2.5 安装 并使用其新建一个maven web项目部署发布
  • 原文地址:https://www.cnblogs.com/start1225/p/6725630.html
Copyright © 2011-2022 走看看