策略模式
策略模式是把 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 "查询不到该优惠券的发放方式"; } }