设计模式-策略模式

目录

1.初步认识

2.角色功能

3.代码实现

4.优缺点

1.初步认识

  • 策略模式(Strategy Pattern):定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换

2.角色功能

  • Context上下文:屏蔽高层模块对策略、算法的直接访问,封装可能存在的变化
  • Strategy策略角色:抽象策略角色,是对策略、算法家族的抽象,定义每个策略或算法必须具有的方法和属性
  • ConcreteStrategy具体策略角色:用于实现抽象策略中的操作,即实现具体的算法

3.代码实现

实体类

java 复制代码
/**
 * 实体类
 */
public class Order {

    private double price;
    private int userId;
    private int productId;

    public Order() {
    }

    public Order(double price, int userId, int productId) {
        this.price = price;
        this.userId = userId;
        this.productId = productId;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

抽象类

java 复制代码
/**
 * 抽象类
 */
public abstract class Strategy {

    //计算价格抽象方法,计算折扣价格
    public abstract double computePrice(Order order);
}

上下文

java 复制代码
/**
 * @className: PromotionContext
 * @author: 会敲代码的小张
 * @date: 2024/9/10 11:55
 * @Version: 1.0
 * @description: 策略上下文
 */
public class PromotionContext {

    private Strategy strategy;

    public PromotionContext(Strategy strategy) {
        this.strategy = strategy;
    }

    /**
     * 根据策略计算最终价格
     * @param order
     * @return
     */
    public double execute(Order order){
        return strategy.computePrice(order);
    }
}

具体的策略

java 复制代码
/**
 * @className: NormalActivity
 * @author: 会敲代码的小张
 * @date: 2024/9/10 11:57
 * @Version: 1.0
 * @description: 具体的策略-1-正常价格
 */
public class NormalActivity extends Strategy{


    @Override
    public double computePrice(Order order) {
        return order.getPrice();
    }
}


/**
 * @className: DisCountActivity
 * @author: 会敲代码的小张
 * @date: 2024/9/10 11:58
 * @Version: 1.0
 * @description: 具体策略-2-打折价格
 */
public class DisCountActivity extends Strategy {

    //具体折扣
    private double rate;

    public DisCountActivity(double rate) {
        this.rate = rate;
    }

    @Override
    public double computePrice(Order order) {
        return order.getPrice() * rate;
    }
}

4.优缺点

  • 优点:
    • 满足开闭原则,当增加新的具体策略时,不需要修改上下文类的代码,上下文就可以引用新的具体策略的实例
    • 避免使用多重条件判断,如果不用策略模式可能会使用多重条件语句不利于维护,和工厂模式的搭配使用
  • 缺点:
    • 策略类数量会增多,每个策略都是一个类,复用的可能性很小
    • 对外暴露了类所有的行为和算法,行为过多导致策略类膨胀
相关推荐
泡^泡20 分钟前
Spring AI简单高仿DeepSeek问答页面
java·人工智能·spring
聚名网24 分钟前
域名net,com,cn有区别吗?有哪些不同呢?
服务器·开发语言·php
牛油果子哥q26 分钟前
STL set与map底层精讲,红黑树适配原理、有序去重特性、迭代器遍历、API实战与面试核心考点全解
开发语言·数据结构·c++·面试
foundbug99928 分钟前
直流电机 PID 速度控制 MATLAB 仿真程序
开发语言·matlab
带刺的坐椅31 分钟前
Solon v4.0 正式发布,高考记忆版
java·ai·solon·flow·solon-ai
山东点狮信息科技有限公司1 小时前
企业级 MES 制造执行系统架构设计与实践
spring cloud·性能优化·系统架构·策略模式·点狮
MartinYeung51 小时前
[论文学习]DP2Unlearning:高效且具保证的大型语言模型遗忘框架(基于差分隐私的 LLM Unlearning 方法)
学习·算法·语言模型
Tian_Hang2 小时前
C++原型模式(Protype)
开发语言·c++·算法
bIo7lyA8v2 小时前
算法复杂度的渐进分析与实际运行时间的差异的技术8
算法