如何将业务代码写得像诗一样(使用注解+单例+工厂去掉一大波if和else判断)
1.订单控制器,提供一个根据商品id和银行渠道id计算商品折后价格的接口:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; @RestController
@RequestMapping("/order")
public class OrderController { /**
* 根据商品id和银行渠道id计算折扣后的金额
*
* @param goodsId 商品id
* @param channelId 银行渠道id
* @return
*/
@GetMapping("/calc")
@ResponseBody
public String calcAmount(Integer goodsId, Integer channelId) {
Context context = new Context();
BigDecimal bigDecimal;
try {
bigDecimal = context.calRecharge(goodsId, channelId);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return bigDecimal.setScale(2) + "";
}
}
2.上下文:
import java.math.BigDecimal; public class Context { /**
* 根据商品id和银行渠道id计算折扣后的金额
*
* @param goodsId 商品id
* @param channelId 银行渠道id
* @return
* @throws Exception
*/
public BigDecimal calRecharge(Integer goodsId, Integer channelId) throws Exception {
StrategyFactory strategyFactory = StrategyFactory.getInstance();
// 根据渠道id查询具体的银行实现类
Strategy strategy = strategyFactory.create(channelId);
// 调用具体的实现类进行计算
return strategy.calRecharge(goodsId, channelId);
}
}
3.折扣计算单例工厂类,内部用一个Map来存储银行渠道id和具体银行实现类之间的映射关系,方便根据渠道id反射获取对应银行具体的实现类:
import org.reflections.Reflections; import java.util.HashMap;
import java.util.Set; public class StrategyFactory {
private static StrategyFactory strategyFactory = new StrategyFactory(); private StrategyFactory() {
} public static StrategyFactory getInstance() {
return strategyFactory;
} private static HashMap<Integer, String> source_map = new HashMap<>(); static {
Reflections reflections = new Reflections("ICBCBankImpl");
Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(Pay.class);
for (Class<?> clazz : classSet) {
Pay pay = clazz.getAnnotation(Pay.class);
source_map.put(pay.channelId(), clazz.getCanonicalName());
}
} /**
* 根据银行渠道id从Map中获取具体的银行实现类
*
* @param channelId
* @return
* @throws Exception
*/
public Strategy create(int channelId) throws Exception {
String clazz = source_map.get(channelId);
Class<?> clazz_ = Class.forName(clazz);
return (Strategy) clazz_.newInstance();
}
}
4.计算折后价格的接口:
import java.math.BigDecimal; public interface Strategy {
BigDecimal calRecharge(Integer goodsId, Integer channelId);
}
5.工商银行实现类,类上加上@Pay注解指定工商银行对应的数据库中的渠道id:
import javax.annotation.Resource;
import java.math.BigDecimal; /**
* 工商银行实现类,对应的数据库中的渠道id为1
*/
@Pay(channelId = 1)
public class ICBCBankImpl implements Strategy { @Resource
private GoodsMapper goodsMapper; @Resource
private ChannelMapper channelMapper; /**
* 根据商品id和银行渠道id计算优惠后的价格
*
* @param goodsId 商品id
* @param channelId 银行渠道id
* @return
*/
@Override
public BigDecimal calRecharge(Integer goodsId, Integer channelId) {
BigDecimal goodsPrice = goodsMapper.getGoodsPriceById(goodsId);
BigDecimal discountPrice = channelMapper.getDiscountPriceById(channelId);
if (goodsPrice == null || discountPrice == null) {
return null;
}
return goodsPrice.multiply(discountPrice);
}
}
6.用于标记银行实现类的注解,定义了一个银行渠道id属性:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Pay {
int channelId();
}
7.模拟查询商品价格的Dao:
import java.math.BigDecimal; public class GoodsMapper {
public BigDecimal getGoodsPriceById(Integer goodsId) {
return BigDecimal.valueOf(599);
}
}
8.模拟查询查询渠道优惠折扣的Dao:
import java.math.BigDecimal; public class ChannelMapper {
public BigDecimal getDiscountPriceById(Integer channelId) {
return BigDecimal.valueOf(0.5);
}
}
如何将业务代码写得像诗一样(使用注解+单例+工厂去掉一大波if和else判断)的更多相关文章
- 如何写出面试官欣赏的Java单例
单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 今天我们不谈单例模式的用途,只说一说如果在面试的时候面试官让你敲一段代码 ...
- 类(静态)变量和类(静态)static方法以及main方法、代码块,final方法的使用,单例设计模式
类的加载:时间 1.创建对象实例(new 一个新对象时) 2.创建子类对象实例,父类也会被加载 3.使用类的静态成员时(静态属性,静态方法) 一.static 静态变量:类变量,静态属性(会被该类的所 ...
- golang写业务代码,用全局函数还是成员函数
在golang中,函数划分为全局函数和成员函数,在使用的时候,有种情况,会产生一些疑惑的,就是在写业务代码的时候,使用全局函数好像会比较方便,一般业务代码,都不会复用,都是针对特定的业务进行编程,要复 ...
- jdk1.7推出的Fork/Join提高业务代码处理性能
jdk1.7推出的Fork/Join提高业务代码处理性能 jdk1.7之后推出了Fork/Join框架,其原理个人理解为:递归多线程并发处理业务代码,以下为我模拟我们公司业务代码做的一个案例,性能可提 ...
- 朱晔的互联网架构实践心得S2E2:写业务代码最容易掉的10种坑
我承认,本文的标题有一点标题党,特别是写业务代码,大家因为没有足够重视一些细节最容易调的坑(侧重Java,当然,本文说的这些点很多是不限制于语言的). 1.客户端的使用 我们在使用Redis.Elas ...
- 朱晔的互联网架构实践心得S2E1:业务代码究竟难不难写?
注意,这是我的架构实践心得的第二季的系列文章,第一季有10篇你也可以回顾. 见https://www.cnblogs.com/lovecindywang/category/1296779.html 最 ...
- .net程序员写业务代码需要注意的地方
代码规范要求1.命名空间规范:dao层的impl实现和接口采用一样的命名空间,到对应文件夹层:IxxDaoContext与其实现类采用顶级命名空间. 2.TableEntity文件夹:所有的实体放到各 ...
- CSDN日报20170413 ——《天天写业务代码的那些年,我们是怎样成长过来的》
[程序人生]天天写业务代码的那些年,我们是怎样成长过来的 作者:Phodal 比起写业务代码更不幸的是,主要工作是修 Bug , bug , buG , bUg. [Java 编程]Springboo ...
- 读 Kafka 源码写优雅业务代码:配置类
这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...
随机推荐
- 原生js的常用方法总结
=============== 通知: 博主已迁至<掘金>码字,博客园可能以后不再更新,掘金地址:https://juejin.im/post/5a1a6a6551882534af25a8 ...
- 别名alias永久生效别名alias永久生效;虚拟机的NAT模式,进行静态IP配置,并A、B的实现免密访问
别名alias永久生效 1.打开cd /etc/profile.d 目录 新建文件my_alias.sh 2.my_alias.sh里面添加 alias p=’poweroff -h’ alias r ...
- crontab每小时运行一次(转)
https://blog.csdn.net/liu0808/article/details/80668705 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错 ...
- iOS 应用逆向工程分析流程图
http://bbs.iosre.com/t/ios/12432
- 2019湖南省赛H题——概率转移&&逆矩阵
题意 题目链接 Bobo有一个 $n+m$ 个节点的有向图,编号分别为 $1 \sim n$,他还有一个 $n$ 行 $n+m$ 列的矩阵 $P$. 如果在 $t$ 时刻他位于节点 $u(1 \leq ...
- python - django 项目部署 Ubuntu 服务器后接口访问一直 502 问题
问题描述:最近有了一台 Ubuntu 的服务器,然后准备部署个项目,结果没想到部署的过程跟用 Centos 的时候还有点不一样,最后一步我是卡在了 uwsgi 这里,访问一直502,且可以访问项目的静 ...
- LeetCode 1234. Replace the Substring for Balanced String
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...
- 5-微信小程序开发(小程序页面跳转和布局说明)
https://www.cnblogs.com/yangfengwu/p/11605209.html 新建一个小程序 咱现在新建个页面 在pages 上右击,选择新建目录 会自动添加这几个文件 现在做 ...
- vue单项数据流
当父组件给子组件传递数据的时候,子组件只能读取,不能改写.因为如果子组件改变父组件传递过来的数据时会造成数据流难以理解.
- Zookeeper循环注册监听器
Zookeeper中的监听器只执行一次,需要在watcher类中重写process方法,以达到重复注册监听器的效果 /** * 连接zk服务器 * */ public static void conn ...