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

news/2025/9/26 23:27:47/文章来源:https://www.cnblogs.com/wanggangblog/p/19110500

使用责任链模式简化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代码示例

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/916754.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

织梦手机网站模板下载备份wordpress到百度云

介绍 在很多应用中&#xff0c;会出现点击按钮出现水波纹的特效。 效果图预览 使用说明 进入页面&#xff0c;点击按钮&#xff0c;触发水波纹动画。再次点击按钮&#xff0c;停止水波纹动画。 实现思路 本例涉及的关键特性和实现方案如下&#xff1a; 要实现存在两个连续…

hf 下载模型

hf 下载模型huggingface-cli download --resume-download stable-diffusion-v1-5/stable-diffusion-v1-5 --local-dir .

SQLAlchemy -> Base.metadata.create_all(engine )详解 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

漂亮购物网站欣赏国外网站开发技术现状

UE4_材质节点 2017-12-07 13:56 跑九宫格 跑UV 评论(0)

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

使用Function Interface简化if-else代码示例Posted on 2025-09-25 08:52 刚泡 阅读(0) 评论(0) 收藏 举报使用表驱动的方法,利用Function Interface优化If-else的示例代码:1 package com.siasun.java8.function…

南京网站建设企业装修网站建设策划方案

DataStream API 将你的应用构建为一个 job graph&#xff0c;并附加到 StreamExecutionEnvironment 。当调用 env.execute() 时此 graph 就被打包并发送到 JobManager 上&#xff0c;后者对作业并行处理并将其子任务分发给 Task Manager 来执行。每个作业的并行子任务将在 task…

网站开发人才需求章丘建设局网站

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/135930139 小分子药物生成是一种利用计算方法自动探索化学空间&#xff0c;寻找具有理想生物活性和药物特性的分子结构的过程。从头设计是一种特殊…

个人网站做淘宝客犯法吗wordpress登录数据库吗

代码随想录二刷 &#xff5c;二叉树 &#xff5c; 验证二叉搜索树 题目描述解题思路递归法迭代法 代码实现递归法迭代法 题目描述 98.验证二叉搜索树 给定一个二叉树&#xff0c;判断其是否是一个有效的二叉搜索树。 假设一个二叉搜索树具有如下特征&#xff1a; 节点的左子…

Up

Up粤qy-手动更新对应包体Liunx uname -a(显示内核和系统信息) lsb_release -a(提供发行版详情) cat /etc/os-release(获取操作系统标识)通过v1.4 yqy -> 通过yzy -> 通过u 0_trus-下载对应包体 先提前告知…

Transformer 面试题及详细答案120道(51-60)-- 模型变体与改进 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

广州网站建设培训wordpress调用最新文章插件

Spring AOP实现 AOP概述什么是AOP什么是Spring AOP Spring AOP快速入门引入依赖实现计时器 Spring AOP详解Spring AOP核心概念切点(Pointcut)连接点(Join Point)通知(Advice)切面(Aspect) 通知类型注意事项 PointCut多个切面切面优先级 Order切点表达式execution表达式annotati…

做时尚网站的目的国产99做视频网站

文章目录 一. hive高可用原理说明1. Hive MetaStore HA2. hive server HA 二. hive高可用实现1. 配置2. beeline链接测试3. zookeeper相关操作 一. hive高可用原理说明 1. Hive MetaStore HA Hive元数据存储在MetaStore中&#xff0c;包括表的定义、分区、表的属性等信息。 hi…

上海做网站的哪家好可以看各种直播平台的软件

数仓 olap vs oltp OLTP主要用于支持日常的业务操作&#xff0c;如银行交易、电子商务等&#xff0c;强调数据的准确性、实时性和并发性。OLAP主要用于支持复杂的数据分析&#xff0c;如数据仓库、决策支持等&#xff0c;强调数据的维度、聚合和可视化。 将OLTP数据库的数据…

梁山网站建设电话网站开发现在用什么语言

Python插件PyAutoGui的使用方法 一、控制鼠标二、图片处理三、控制键盘四、其他方法 一、控制鼠标 pyautogui.moveTo(w - 100, h - 100, duration0.25) # 立即移动到指定x&#xff0c; y位置坐标&#xff0c; duration表示移动花费的时间,0表示立即 pyautogui.moveRel(100, 0…

外贸企业网站建设一条龙标智客logo设计免费生成

一&#xff1a;背景 1.讲故事今天给大家带来一个入门级的 CPU 爆高案例&#xff0c;前段时间有位朋友找到我&#xff0c;说他的程序间歇性的 CPU 爆高&#xff0c;不知道是啥情况&#xff0c;让我帮忙看下&#xff0c;既然找到我&#xff0c;那就用 WinDbg 看一下。二&#xff…

教育机构网站建设男女做暖暖不要钱的试看网站

作者简介&#xff1a;大家好&#xff0c;我是未央&#xff1b; 博客首页&#xff1a;未央.303 系列专栏&#xff1a;笔试强训选择题 每日一句&#xff1a;人的一生&#xff0c;可以有所作为的时机只有一次&#xff0c;那就是现在&#xff01;&#xff01;&#xff01;&#xff…

网站 别名零基础怎么学网页设计

3 月 5 日刚召开的两会&#xff0c;AI 这个话题妥妥站上了 C 位。不仅政府工作报告首次提出要开展“人工智能”行动&#xff0c;各路科技大佬和人大代表也是围绕着 AI 大模型的技术创新、应用落地和政策法规&#xff0c;展开了热烈积极的建言献策。甚至有互联网大佬建议将人工智…

Dockerfile构建镜像以及网络 - 详解

Dockerfile构建镜像以及网络 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco…

详细介绍:2026毕设-基于Spring Boot的在线海鲜市场交易平台的设计与实现

详细介绍:2026毕设-基于Spring Boot的在线海鲜市场交易平台的设计与实现pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family:…

【源码解读之 Mybatis】【基础篇】-- 第3篇:SqlSession的创建与生命周期

【源码解读之 Mybatis】【基础篇】-- 第3篇:SqlSession的创建与生命周期第3篇:SqlSession的创建与生命周期 1. 学习目标确认 1.0 第2篇思考题解答 在深入学习SqlSession之前,让我们先回顾并解答第2篇中提出的思考题…