尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

使用责任链模式简化if-else代码示例
📅 发布时间:2026/6/20 16:20:44

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

Posted on 2025-09-25 08:54  刚泡  阅读(0)  评论(0)    收藏  举报
使用责任链模式简化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代码示例

相关新闻

  • SQLAlchemy -> Base.metadata.create_all(engine )详解 - 实践
  • Transformer 面试题及详细答案120道(51-60)-- 模型变体与改进 - 详解
  • 【源码解读之 Mybatis】【基础篇】-- 第3篇:SqlSession的创建与生命周期

最新新闻

  • 2026年抗抑菌剂/消毒产品检测机构推荐:广州市微生物研究所集团专业服务 - 品牌推荐官
  • 2025年厨房家居用品实力厂家推荐:青岛乐博智家密封罐/果盘/冷萃壶全系供应 - 品牌推荐官
  • CentOS 8 LAMP环境搭建与三重加固实战指南
  • 2026年化工原料优质供应商推荐:山东喜玛供应链管理有限公司1,2-丙二醇等全系供应 - 品牌推荐官
  • Fate/Grand Automata:5步掌握F/GO安卓自动战斗工具配置与使用
  • 从MC68HC908AZ60A到MC9S08DZ60:EEPROM、时钟与外设迁移实战指南

日新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号