设计模式-策略模式

作者:京东工业 孙磊

一、概念

策略模式(Strategy Pattern)也称为(Policy Parttern)。 它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变换,不会影响到使用算法的客户。策略模式属性行为模式。

策略模式结构图

二、实际应用

业务场景:业务需要监听多种消息,将接收到的消息更新到同一个ES中,不同的消息类型使用不同的策略处理,补充不同的数据信息,更新到ES中,供商家搜索和统计使用。

代码实现结合spring框架、简单工厂和策略模式一起使用。

java 复制代码
public interface GatherExecuteService {    
    /**     
    * 处理消息体     
    *     
    * @param gatherDataVo     
    */    
    boolean execute(GatherDataVo gatherDataVo);
}

多个实现类

typescript 复制代码
// 价格策略实现
@Service
public class PriceExecuteServiceImpl implements GatherExecuteService {    
    @Override    
    public boolean execute(GatherDataVo gatherDataVo) {
         .....具体实现代码省略   
    }
}
typescript 复制代码
// 商品策略实现
@Service
public class ProductExecuteServiceImpl implements GatherExecuteService {  

    @Override    
    public boolean execute(GatherDataVo gatherDataVo) {  

        .....具体实现代码省略  
    }
}
typescript 复制代码
// 库存策略实现
@Service
public class StockExecuteServiceImpl implements GatherExecuteService {    
    @Override    
    public boolean execute(GatherDataVo gatherDataVo) {   
     .....具体实现代码省略  

     }
}

使用枚举存储策略实现bean

typescript 复制代码
@Getter
@AllArgsConstructor
public enum MessageTypeEnum {    
    PRODUCT(0, "productExecuteServiceImpl", "商品基本信息消息"),    
    PRICE(1, "priceExecuteServiceImpl", "价格消息"),    
    STOCK(2, "stockExecuteServiceImpl", "库存消息") ;    
    private int type;    
    private String service;   
    private String description;    
    public static String getServiceName(int type) {        
        MessageTypeEnum[] typeEnums = MessageTypeEnum.values();        
        for (MessageTypeEnum enumType : typeEnums) {            
            if (enumType.getType() == type) {                
                return enumType.getService();            
            }     
        }
        return null;    
    }
}

使用到不同策略的代码

ini 复制代码
// 根据消息类型获取不同策略类,然后使用spring的ApplicationContext获取bean,达到执行不同策略的目的。
String serviceName = MessageTypeEnum.getServiceName(gatherDataVo.getMessageType());
if (StringUtils.isNotBlank(serviceName)) {  
    GatherExecuteService gatherExecuteService = (GatherExecuteService) SpringContextUtil.getBean(serviceName,                      GatherExecuteService.class);  
}

策略模式是一种比较简单的设计模式,工作中经常和其他设计模式一块使用。简单的应用记录分享一下。

相关推荐
AI人工智能+电脑小能手6 分钟前
【大白话说Java面试题 第47题】【JVM篇】第7题:Young GC 和 Full GC 分别采用什么算法?
java·jvm·后端·算法·面试
lyp90h8 分钟前
Claude Code CLI System Prompt 完整分析
java·前端·prompt
xu_ws8 分钟前
redis的io多路复用和Java NIO的区别
java·redis·nio
Hesionberger14 分钟前
LeetCode98:验证二叉搜索树(多解)
java·开发语言·python·算法·leetcode·职场和发展
千寻girling15 分钟前
周日那天参加的力扣周赛... —— 10号
java·javascript·c++·python·算法·leetcode·职场和发展
凛_Lin~~16 分钟前
lifecycle源码解析 (版本2.5.1)
android·java·安卓·lifecycle
Devin~Y17 分钟前
大厂Java面试实录:Spring Boot微服务 + Redis/Kafka + Prometheus/Jaeger + RAG/Agent(小Y水货版)
java·spring boot·redis·spring cloud·kafka·prometheus·jaeger
zhoumeina9918 分钟前
设计器模版底图,一直渲染错误,是因为第一张图变形后内存中图片数据被改了,其他尺码一直错误
java·前端·javascript
Mike117.19 分钟前
GBase 8c 会话、锁等待和长 SQL 的日常巡检写法
java·数据库·sql
逻辑驱动的ken19 分钟前
Java高频面试考点场景题28
java·开发语言·面试·职场和发展·求职招聘