zoukankan      html  css  js  c++  java
  • 设计模式(五)责任链模式

    一.什么是责任链模式?

        责任链就是从一个起点发起请求,然后沿着任务链依次传递给每一个节点上的对象,直到有一个节点处理这个请求为止。

    二.责任链模式实例实现?

     1 public abstract class ConsumeHandler {  
     2     private ConsumeHandler nextHandler;  
     3   
     4     public ConsumeHandler getNextHandler(){  
     5         return nextHandler;  
     6     }  
     7   
     8     public void setNextHandler(ConsumeHandler nextHandler){  
     9         this.nextHandler=nextHandler;  
    10     }  
    11   
    12     /** 
    13      * user申请人  free报销费用 
    14      */  
    15     public abstract void doHandler(String user,double free);  
    16 }  
    17 
    18 //项目经理  
    19 public class ProjectHandler extends ConsumeHandler {  
    20     @Override  
    21     public void doHandler(String user, double free) {  
    22         if (free < 500){  
    23             if (user.equals("lwx")){  
    24                 System.out.print("给予报销:"+free);  
    25             }else{  
    26                 System.out.print("报销不通过");  
    27             }  
    28         }else{  
    29             if (getNextHandler()!=null){  
    30                 getNextHandler().doHandler(user,free);  
    31             }  
    32         }  
    33     }  
    34 }  
    35 
    36 //部门经理  
    37 public class DeptHandler extends ConsumeHandler {  
    38     @Override  
    39     public void doHandler(String user, double free) {  
    40         if (free<1000){  
    41             if (user.equals("zy")){  
    42                 System.out.print("给予报销:"+free);  
    43             }else{  
    44                 System.out.println("报销不通过");  
    45             }  
    46         }else{  
    47             if (getNextHandler()!=null){  
    48                 getNextHandler().doHandler(user,free);  
    49             }  
    50         }  
    51     }  
    52 }  
    53 
    54 //总经理 
    55 public class GeneralHandler extends ConsumeHandler {  
    56     @Override  
    57     public void doHandler(String user, double free) {  
    58         if (free>=1000){  
    59             if (user.equals("lwxzy")) {  
    60                 System.out.println("给予报销:" + free);  
    61             } else {  
    62                 System.out.println("报销不通过");  
    63             }  
    64         }else{  
    65             if (getNextHandler()!=null){  
    66                 getNextHandler().doHandler(user,free);  
    67             }  
    68         }  
    69     }  
    70 }  
    71 
    72 public class ChainOfResponsibilityActivity extends AppCompatActivity {  
    73   
    74     @Override  
    75     protected void onCreate(Bundle savedInstanceState) {  
    76         super.onCreate(savedInstanceState);  
    77         setContentView(R.layout.activity_chain_of_responsibility);  
    78   
    79         ProjectHandler projectHandler=new ProjectHandler();  
    80         DeptHandler deptHandler=new DeptHandler();  
    81         GeneralHandler generalHandler=new GeneralHandler();  
    82   
    83         projectHandler.setNextHandler(deptHandler);  
    84         deptHandler.setNextHandler(generalHandler);  
    85   
    86         projectHandler.doHandler("lwx",450);  
    87         projectHandler.doHandler("lwx", 600);  
    88         projectHandler.doHandler("zy", 600);  
    89         projectHandler.doHandler("zy", 1500);  
    90         projectHandler.doHandler("lwxzy", 1500);  
    91     }  
    92 }
  • 相关阅读:
    Spring session redis
    HttpClient Post Form data and get Response String
    compareTo & toString
    Zookeeper 脑裂
    Maven的HTTP代理设置
    Linux 硬件信息
    [Redis] RDB & AOF
    真实场景中WebRTC 用到的服务 STUN, TURN 和 signaling
    推荐一个VS2015 插件 Favorite Documents
    笔记 Activator.CreateInstance(Type)
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/7198084.html
Copyright © 2011-2022 走看看