设计模式-策略模式

目录

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.优缺点

  • 优点:
    • 满足开闭原则,当增加新的具体策略时,不需要修改上下文类的代码,上下文就可以引用新的具体策略的实例
    • 避免使用多重条件判断,如果不用策略模式可能会使用多重条件语句不利于维护,和工厂模式的搭配使用
  • 缺点:
    • 策略类数量会增多,每个策略都是一个类,复用的可能性很小
    • 对外暴露了类所有的行为和算法,行为过多导致策略类膨胀
相关推荐
焜昱错眩..16 分钟前
代码随想录算法训练营第三十九天|62.不同路径 63.不同路径ll
算法
float_六七2 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天3 小时前
JAVA stream().flatMap()
java·windows
焦耳加热4 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
CodeCraft Studio4 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一4 小时前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
wan5555cn4 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
颜如玉4 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
u6064 小时前
常用排序算法核心知识点梳理
算法·排序
程序员爱钓鱼5 小时前
Go语言实战案例 — 工具开发篇:实现一个图片批量压缩工具
后端·google·go