使用责任链模式简化if-else代码示例

使用责任链模式简化if-else代码示例:
  1 package com.siasun.java8.function.responsibility;
  2 
  3 import java.math.BigDecimal;
  4 
  5 /**
  6  * 使用责任链模式简化if-else代码
  7  * @author : wanggang
  8  * @create 2025/7/15 9:01
  9  */
 10 public class ChainofResponsibilityDemo {
 11 
 12   //定义责任链
 13   public static Handler handler = new Handler1();
 14 
 15   static {
 16     Handler handler2 = new Handler2();
 17 
 18     Handler handler3 = new Handler3();
 19     handler.setNextHandler(handler2);
 20 
 21     handler2.setNextHandler(handler3);
 22   }
 23 
 24   public static void main(String[] args) {
 25     BigDecimal yearIncome1 = new BigDecimal(28000);
 26 
 27     BigDecimal tax1 = handler.handleRequest(yearIncome1);
 28 
 29     System.out.println("个人所得税为:" + tax1);
 30 
 31     BigDecimal yearIncome2 = new BigDecimal(124000);
 32 
 33     BigDecimal tax2 = handler.handleRequest(yearIncome2);
 34 
 35     System.out.println("个人所得税为:" + tax2);
 36 
 37     BigDecimal yearIncome3 = new BigDecimal(180000);
 38 
 39     BigDecimal tax3 = handler.handleRequest(yearIncome3);
 40 
 41     System.out.println("个人所得税为:" + tax3);
 42   }
 43 
 44   public abstract static class Handler {
 45 
 46     protected Handler nextHandler;
 47 
 48     public void setNextHandler(Handler nextHandler) {
 49       this.nextHandler = nextHandler;
 50     }
 51 
 52     public abstract BigDecimal handleRequest(BigDecimal yearIncome);
 53 
 54     public abstract boolean canHandle(BigDecimal yearIncome);
 55   }
 56 
 57   public static class Handler1 extends Handler {
 58 
 59     @Override
 60     public BigDecimal handleRequest(BigDecimal yearIncome) {
 61       if (canHandle(yearIncome)) {
 62         return yearIncome.multiply(BigDecimal.valueOf(0.1));
 63       } else {
 64         if (null != nextHandler) {
 65           return nextHandler.handleRequest(yearIncome);
 66         } else {
 67           return BigDecimal.ZERO;
 68         }
 69       }
 70     }
 71 
 72     @Override
 73     public boolean canHandle(BigDecimal yearIncome) { // 处理36000以下的
 74       return yearIncome.compareTo(BigDecimal.valueOf(36000)) < 0;
 75     }
 76   }
 77 
 78   public static class Handler2 extends Handler {
 79 
 80     @Override
 81     public BigDecimal handleRequest(BigDecimal yearIncome) {
 82       if (canHandle(yearIncome)) {
 83         return yearIncome.multiply(BigDecimal.valueOf(0.2));
 84       } else {
 85         if (null != nextHandler) {
 86           return nextHandler.handleRequest(yearIncome);
 87         } else {
 88           return BigDecimal.ZERO;
 89         }
 90       }
 91     }
 92 
 93     @Override
 94     public boolean canHandle(BigDecimal yearIncome) { // 36000(含)-144000(不含)
 95       return (
 96         yearIncome.compareTo(BigDecimal.valueOf(36000)) >= 0 &&
 97         yearIncome.compareTo(BigDecimal.valueOf(144000)) < 0
 98       );
 99     }
100   }
101 
102   public static class Handler3 extends Handler {
103 
104     @Override
105     public BigDecimal handleRequest(BigDecimal yearIncome) {
106       if (canHandle(yearIncome)) {
107         return yearIncome.multiply(BigDecimal.valueOf(0.3));
108       } else {
109         if (null != nextHandler) {
110           return nextHandler.handleRequest(yearIncome);
111         } else {
112           return BigDecimal.ZERO;
113         }
114       }
115     }
116 
117     @Override
118     public boolean canHandle(BigDecimal yearIncome) { // 大于等于144000
119       return yearIncome.compareTo(BigDecimal.valueOf(144000)) >= 0;
120     }
121   }
122 }

 

使用Function Interface简化if-else代码示例

posted @ 2025-09-25 08:54  刚泡  阅读(8)  评论(0)    收藏  举报