策略模式就是定义一组策略,分别在不同的类中封装,每种策略可以根据当前的场景互相替换,策略的之间的变化是独立的。比如我们要从杭州到上海,可以根据自身的情况,来选择是乘坐火车、汽车、高铁,这些出行方式就是不同的策略。
当代码中的if else 超过3层情况,是时候考虑使用策略模式的代替了,好的代码是符合单一职责和开闭原则的。
1、获取策略方法
2、是否执行策略逻辑,这个后面方便管理
3、处理策略逻辑的方法
package com.example.caffe.hello.biz.designmode.strategy; /** * 策略接口 */ public interface ItemHandler<T,R> { /** * 商品的标识 * @return */ String getItemCode(); /** * 是否执行 * @param context * @return */ boolean needExecute(T context); /** * 执行 * @param context * @return */ R execute(T context); }
package com.example.caffe.hello.biz.designmode.strategy; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Component @Slf4j public class NormalItemHandler implements ItemHandler<ItemRequestContext,ItemResponseDTO>{ @Override public String getItemCode() { return "normalItem"; } @Override public boolean needExecute(ItemRequestContext context) { return true; } @Override public ItemResponseDTO execute(ItemRequestContext context) { log.info("NormalItemHandler execute...."); ItemResponseDTO responseDTO = new ItemResponseDTO(); responseDTO.setSuccess(true); return responseDTO; } }
package com.example.caffe.hello.biz.designmode.strategy; import lombok.Data; /** * 策略的执行上下文 */ @Data public class ItemRequestContext { private String itemCode; }
package com.example.caffe.hello.biz.designmode.strategy; import lombok.Data; /** * 策略的响应结果 */ @Data public class ItemResponseDTO { private boolean success; }
package com.example.caffe.hello.biz.designmode.strategy; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @Slf4j public class ItemHandlerFactory { private static final Map<String,ItemHandler> ITEM_HANDLER_MAP = new HashMap<>(16); /** * 根据商品code,返回对应的handler * @param itemCode * @return */ public ItemHandler getHandler(String itemCode) { return ITEM_HANDLER_MAP.get(itemCode); } @Autowired public void setItemHandlers(List<ItemHandler> handlers) { if (CollectionUtils.isEmpty(handlers)) { return; } for (ItemHandler handler : handlers) { log.info("init handler={},success",handler.getItemCode()); System.out.println("init handler="+handler.getItemCode()+",success"); ITEM_HANDLER_MAP.put(handler.getItemCode(),handler); } } }
基于spring 容器,将所有的ItemHandler自动注册到ITEM_HANDLER_MAP,从而Spring容器启动完成后,getHandler 方法可以直接通过itemCode获取对应的ItemHandler。
package com.example.caffe.hello.biz.designmode.strategy; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component public class ItemProcessService { @Resource private ItemHandlerFactory itemHandlerFactory; public ItemResponseDTO processItem() { ItemRequestContext itemRequestContext = new ItemRequestContext(); ItemHandler itemHandler = itemHandlerFactory.getHandler(itemRequestContext.getItemCode()); if (itemHandler.needExecute(itemRequestContext)) { return (ItemResponseDTO) itemHandler.execute(itemRequestContext); } return null; } }
Factory 职责是获取handler,hanler 只负责处理具体的逻辑,service 只负责逻辑编排,从而实现”低耦合高内聚“。至此策略模式介绍完了,