Map+函数式接口解决 if-else
策略模式
策略模式是把 if语句里面的逻辑抽出来写成一个类,如果要修改某个逻辑的话,仅修改一个具体的实现类的逻辑即可,可维护性会好不少。
以下是策略模式的具体结构
switch(resourceType){
case "红包":
String grantType=new Context(new RedPaper()).ContextInterface();
break;
case "购物券":
String grantType=new Context(new Shopping()).ContextInterface();
break;
......
default : logger.info("查找不到该优惠券类型resourceType以及对应的派发方式");
break;
但缺点也明显:
- 如果 if-else的判断情况很多,那么对应的具体策略实现类也会很多,上边的具体的策略实现类还只是2个,查询红包发放方式写在类RedPaper里边,购物券写在另一个类Shopping里边;那资源类型多个QQ会员和外卖会员,不就得再多写两个类?有点麻烦了
- 没法俯视整个分派的业务逻辑
用上了Java8的新特性lambda表达式
- 判断条件放在key中
- 对应的业务逻辑放在value中
这样子写的好处是非常直观,能直接看到判断条件对应的业务逻辑
需求:根据优惠券(资源)类型resourceType和编码resourceId查询派发方式grantType
@Service public class QueryGrantTypeService { @Autowired private GrantTypeSerive grantTypeSerive; private Map<String, Function<String,String>> grantTypeMap=new HashMap<>(); /** * 初始化业务分派逻辑,代替了if-else部分 * key: 优惠券类型 * value: lambda表达式,最终会获得该优惠券的发放方式 */ @PostConstruct public void dispatcherInit(){ grantTypeMap.put("红包",resourceId->grantTypeSerive.redPaper(resourceId)); grantTypeMap.put("购物券",resourceId->grantTypeSerive.shopping(resourceId)); grantTypeMap.put("qq会员",resourceId->grantTypeSerive.QQVip(resourceId)); } public String getResult(String resourceType){ //Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType Function<String,String> result=getGrantTypeMap.get(resourceType); if(result!=null){ //传入resourceId 执行这段表达式获得String型的grantType return result.apply(resourceId); } return "查询不到该优惠券的发放方式"; } }
---------------------------------------------------------------------------
国之殇,未敢忘!
南京大屠杀!
731部队!
以及核污染水排海等一系列全无人性的操作,购买他们的食品和为它们提供帮助只会更加变本加厉的害你,呼吁大家不要购买日本相关产品
昭昭前事,惕惕后人
吾辈当自强,方使国不受他人之侮!
---------------------------------------------------------------------------
作者:三号小玩家
出处:https://www.cnblogs.com/q1359720840/
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 版权信息