首页 > 其他分享 >设计模式之~策略模式

设计模式之~策略模式

时间:2023-10-18 15:24:44浏览次数:27  
标签:return 策略 模式 adClueDetail ReportStrategyFactory key 设计模式 public

策略模式是属于设计模式中的行为模式中的一种,策略模式主要解决选项过多的问题,避免大量的if else 和 switch下有太多的case。

策略模式的重心不是如何实现算法,而是如何组织、调用这些算法,从而让程序结构更灵活,具有更好的维护性和扩展性。

1.创建抽象策略接口

public interface ReportStrategy extends InitializingBean {
    /**
     * 构建上报数据,不同广告商、不同阶段构造的数据是不同的
     * @param clueDetail
     * @return
     */
    <T extends ReportReqBO> T buildReturnData(AdClueDetail clueDetail);

    /**
     * 上报
     * @param data
     * @param <T>
     * @return
     */
    <T extends ReportReqBO> Object report(T data);
}

 

2.创建策略工厂

@Slf4j
@Getter
public class ReportStrategyFactory {
/** * 上报策略map */ private final Map<String, ReportStrategy> reportStrategyMap = new ConcurrentHashMap<>(16); private static final ReportStrategyFactory INSTANCE = new ReportStrategyFactory(); private ReportStrategyFactory() { }
public static ReportStrategyFactory getInstance() { return INSTANCE; } /** * 构建上报策略的key * @param reportTarget * @param stageType * @return */ public String buildReportKey(ReportTargetEnum reportTarget, StageTypeEnum stageType) { if (reportTarget == null || stageType == null) { log.warn("【策略key不能为空】,reportTarget:{},stageType:{}", reportTarget, stageType); return null; } return reportTarget.getKey().concat(Constants.DELIMITER_DOT).concat(stageType.getKey()); }

 

3.创建接口实现类

@Slf4j
public abstract class DefaultReportService implements ReportStrategy {
@Autowired private ReportService reportService; @Overridepublic KuaiShouClueBackReqBO buildReturnData(AdClueDetail adClueDetail) {
     assemblyParams(adClueDetail,backReqDTO); return kuaiShouClueBackReqBO; } @Override public <T extends ReportReqBO> Object report(T data) { return updateResponseDTO; } /** * 组装回传参数 Assembly parameters * * @param adClueDetail adClueDetail * @param backReqDTO backReqDTO * @return key */ public abstract void assemblyParams(AdClueDetail adClueDetail, KuaiShouBackReqDTO backReqDTO); /** * 获取阶段 * * @return 阶段 */ public abstract StageTypeEnum getStage(); }

3.(1)具体策略A

@Slf4j
@Component
public class ClueIntentionStrategy extends DefaultReportService {
    
    @Override
    public void assemblyParams(AdClueDetail adClueDetail, KuaiShouBackReqDTO backReqDTO) {
        // 做你的业务
    }

    @Override
    public StageTypeEnum getStage() {
        return StageTypeEnum.CLUE_INTENTION;
    }

  /**
    * 把策略放入map
    */
    @Override
    public void afterPropertiesSet() throws Exception {
        ReportStrategyFactory reportStrategyFactory = ReportStrategyFactory.getInstance();
        String key = reportStrategyFactory.buildReportKey(ReportTargetEnum.KUAISHOU, getStage());
        reportStrategyFactory.getReportStrategyMap().put(key, this);
    }
}

3.(2)具体策略B

@Slf4j
@Component
public class ClueValidityStrategy extends DefaultReportService {
    
    @Override
    public void assemblyParams(AdClueDetail adClueDetail, KuaiShouBackReqDTO backReqDTO) {
        // 做你的业务
    }

    @Override
    public StageTypeEnum getStage() {
        return StageTypeEnum.CLUE_VALIDITY;
    }

    /**
    * 把策略放入map
    */
    @Override
    public void afterPropertiesSet() throws Exception {
        ReportStrategyFactory reportStrategyFactory = ReportStrategyFactory.getInstance();
        String key = reportStrategyFactory.buildReportKey(ReportTargetEnum.KUAISHOU, getStage());
        reportStrategyFactory.getReportStrategyMap().put(key, this);
    }
}

1.优点

  • 算法可以自由切换
  • 避免使用多重条件判断(如果不用策略模式我们可能会使用多重条件语句,不利于维护)
  • 扩展性良好,增加一个策略只需实现接口即可

2.缺点

  • 策略类数量会增多,每个策略都是一个类,复用的可能性很小
  • 所有的策略类都需要对外暴露

 

标签:return,策略,模式,adClueDetail,ReportStrategyFactory,key,设计模式,public
From: https://www.cnblogs.com/chuhecc/p/17772444.html

相关文章

  • 10月18日元类、单例模式
    目录1.元类复习1.什么是元类:2.所有类的元类是谁?3.如何自定义元类呢?4.__init__和__new__和__call__这三者的关系:2.单例模式1.元类复习1.什么是元类:因为一切皆对象这个思想,所以类也是对象,元类构造类,类是由元类实例化得到的2.所有类的元类是谁?是type,它是所有......
  • 突破传统投资模式:发掘现货多元化投资平台的优势
    在过去,投资往往被视为许多人眼中的高门槛领域。传统的股票、债券等金融产品常常需要大量资金和专业知识,限制了一般投资者的参与度。然而,随着科技的发展和互联网的普及,现货多元化投资平台逐渐崭露头角,为投资者提供了更加灵活和开放的投资方式。现货多元化投资平台是指通过互联网平......
  • 10月18日单例模式
    目录单例模式值类的绑定方法,装饰器方式设计模式:实现单例模式的第一种方式第一种方式以类的绑定方法来实现实现单例模式的第二种方式实现单例模式的第三种方式单例模式的核心概念是:只有一个实例对象,而不管有多少人尝试访问它。第四种方法,通过模块导入(python的模块就是单例的体现)......
  • 设计模式(八)组合
    一、定义组合多个对象形成树形结构以表示具有部分-整体关系的层次结构。组合模式让客户端可以统一对待单个对象和组合对象。组合模式是一种结构型模式。二、描述包含以下三个角色:1、Component(抽象构件):它可以是接口或抽象类,为叶子构件和容器构件对象声明接口,在该角色中可以包含......
  • go语言使用单例模式封装数据库连接池
    packagesingledbimport( "gorm.io/driver/mysql" "gorm.io/gorm" "sync")//数据库连接对象只有一个var( db*gorm.DB Oncesync.Once//只执行一次某个操作的机制)funcGetDbInstance()*gorm.DB{ Once.Do(func(){ varerrerror dsn:=&q......
  • 职责链模式
    职责链模式案例引入OA(OfficeAutomation)系统的采购审批项目,需求是1.采购员采购教学用品。2.price>=0&&price<=5000由教学主任审批3.price>5000&&price<=10000由原则审批4.price>10000&&price<=30000由副校长审批5.price>30000由校长审批传统方式......
  • c#设计模式-行为型模式 之 备忘录模式
    ......
  • 当防火墙开通策略后如何验证端口服务已经连通了?
    当防火墙开通策略后如何验证端口服务已经连通了?假设策略开通的没有问题。在源主机上进行测试:1.Windows下测试TCP端口格式:telnet【目的IP/域名】端口telnetwww.baidu.com443成功则会显示以下界面 telnetwww.baidu.com135失败则会显示以下界面 2.在Linux(cent......
  • VisualStadio Debug模式突然变慢
    先说解决方式:删除工程目录下隐藏的.vs文件,解决方式内容来源: https://learn.microsoft.com/en-us/answers/questions/1289889/visual-studio-2022-debug-is-very-slow 曾经Debug模式一直很快的,数据都是毫秒级回复。但是突然某天,发现数据响应特别慢,刚开始以为出什么问题了。后......
  • 享元模式--Java实现
    画类图在围棋中,黑棋和白棋对象均只有一个,但是它们可以在不同的位置进行共享;具体代码实现//Chess.javapackageorg.example.design010;publicabstractclassChess{publicabstractStringgetColor();publicvoidlocate(Coordinatesco){System.out.......