• 设计模式- 策略模式(Strategy Pattern)结构|原理|优缺点|场景|示例


                                         设计模式(分类)        设计模式(六大原则)   

        创建型(5种)        工厂方法         抽象工厂模式        单例模式        建造者模式        原型模式

        结构型(7种)        适配器模式        装饰器模式        代理模式        ​​​​​​外观模式      桥接模式        组合模式       享元模式

        行为型(11种)      策略模式        模板方法模式        观察者模式        迭代器模式        责任链模式        命令模式

                                       备忘录模式          状态模式          访问者模式        中介者模式


    设计模式中的策略模式(Strategy Pattern)是一种行为设计模式,它定义了一系列算法,并将每个算法封装在一个单独的类中,使得它们可以互相替换。策略模式使得算法可以在运行时根据需要动态地改变,同时客户端代码可以通过统一的接口调用不同的策略实现。

    模式结构

    1. 策略接口(Strategy Interface)

      • 定义所有支持的策略或算法所共有的方法签名,这是所有具体策略类的抽象父类或接口。
    2. 具体策略类(Concrete Strategies)

      • 每个具体策略类实现了策略接口,并提供了算法的具体实现。
      • 在具体策略类中包含了算法的详细逻辑。
    3. 上下文(Context)

      • 上下文是使用策略的对象,它维持对策略对象的引用,并定义了如何使用策略的方法。
      • 上下文可以根据需求改变策略,通常是通过策略接口设置具体的策略对象。

    工作原理

    • 客户端:创建并配置上下文对象,指定要使用的具体策略。
    • 上下文:根据客户端的配置,保存一个指向具体策略对象的引用,并在需要执行策略时调用策略接口定义的方法。
    • 具体策略:执行实际的算法或行为。

    优缺点

    优点
    • 开放封闭原则:策略模式允许在不修改现有代码的基础上新增策略。
    • 多态性:客户端通过策略接口调用方法,无需关注具体实现细节,增强了代码的灵活性和可扩展性。
    • 解耦:策略模式将算法从使用它的上下文中解耦出来,便于算法的独立管理和测试。
    缺点
    • 策略类数量增多:随着策略数量的增加,可能会产生大量的策略类。
    • 上下文需了解策略:虽然上下文不用关心策略的具体实现,但是它至少需要知道有哪些策略可供选择,并能够适配不同的策略。

    适用场景

    • 系统需要多种算法解决同一问题,且在运行时可以动态切换算法
    • 算法的实现可以相互独立,互不影响
    • 希望避免使用多重条件判断(例如 switch-case 或 if-else)来选择算法

    代码示例(以Java为例)

    1. // 抽象策略接口
    2. public interface DiscountStrategy {
    3. double getDiscount(double price);
    4. }
    5. // 具体策略类 - 无折扣策略
    6. public class NoDiscountStrategy implements DiscountStrategy {
    7. @Override
    8. public double getDiscount(double price) {
    9. return price;
    10. }
    11. }
    12. // 具体策略类 - 普通会员折扣策略
    13. public class NormalMemberDiscountStrategy implements DiscountStrategy {
    14. @Override
    15. public double getDiscount(double price) {
    16. return price * 0.95; // 九五折
    17. }
    18. }
    19. // 具体策略类 - VIP会员折扣策略
    20. public class VIPDiscountStrategy implements DiscountStrategy {
    21. @Override
    22. public double getDiscount(double price) {
    23. return price * 0.85; // 八五折
    24. }
    25. }
    26. // 上下文类 - 订单类,使用策略来计算折扣后的价格
    27. public class Order {
    28. private DiscountStrategy discountStrategy;
    29. public Order(DiscountStrategy discountStrategy) {
    30. this.discountStrategy = discountStrategy;
    31. }
    32. public void setDiscountStrategy(DiscountStrategy discountStrategy) {
    33. this.discountStrategy = discountStrategy;
    34. }
    35. public double calculateFinalPrice(double originalPrice) {
    36. return discountStrategy.getDiscount(originalPrice);
    37. }
    38. }
    39. // 客户端代码
    40. public class Client {
    41. public static void main(String[] args) {
    42. Order orderWithoutDiscount = new Order(new NoDiscountStrategy());
    43. System.out.println("No discount applied: " + orderWithoutDiscount.calculateFinalPrice(100));
    44. Order normalOrder = new Order(new NormalMemberDiscountStrategy());
    45. System.out.println("Normal member discount applied: " + normalOrder.calculateFinalPrice(100));
    46. Order vipOrder = new Order(new VIPDiscountStrategy());
    47. System.out.println("VIP member discount applied: " + vipOrder.calculateFinalPrice(100));
    48. }
    49. }

    代码示例(以Python为例)

    1. # 策略接口
    2. from abc import ABC, abstractmethod
    3. class PaymentStrategy(ABC):
    4. @abstractmethod
    5. def pay(self, amount: float) -> None:
    6. pass
    7. # 具体策略类
    8. class CreditCardPayment(PaymentStrategy):
    9. def __init__(self, card_number: str, cvv: str):
    10. self.card_number = card_number
    11. self.cvv = cvv
    12. def pay(self, amount: float) -> None:
    13. print(f"Paid {amount} using credit card ({self.card_number})")
    14. class PayPalPayment(PaymentStrategy):
    15. def __init__(self, account_id: str):
    16. self.account_id = account_id
    17. def pay(self, amount: float) -> None:
    18. print(f"Paid {amount} using PayPal account ({self.account_id})")
    19. # 上下文
    20. class ShoppingCart:
    21. def __init__(self, payment_strategy: PaymentStrategy):
    22. self.payment_strategy = payment_strategy
    23. def set_payment_strategy(self, strategy: PaymentStrategy) -> None:
    24. self.payment_strategy = strategy
    25. def checkout(self, total_amount: float) -> None:
    26. print(f"Checking out with total amount: {total_amount}")
    27. self.payment_strategy.pay(total_amount)
    28. # 客户端代码
    29. cart = ShoppingCart(CreditCardPayment("1234567890123456", "123"))
    30. cart.checkout(100.00)
    31. cart.set_payment_strategy(PayPalPayment("buyer@example.com"))
    32. cart.checkout(200.00)

    在这个示例中:

    • PaymentStrategy是策略接口,定义了支付方法pay
    • CreditCardPaymentPayPalPayment是具体策略类,实现了支付方式。
    • ShoppingCart是上下文,持有一个支付策略对象,并在其checkout方法中调用策略对象的pay方法来完成支付。根据需要,可以随时更改支付策略。
  • 相关阅读:
    English语法_介词 - of
    微信小程序云开发 | 插件的微信小程序云开发
    门阀-bitlocker
    【Linux】Linux远程访问Windows下的MySQL数据库
    sprigboot+在线预定车位管理 毕业设计-附源码221738
    游戏开发27课 速独算法
    如何在Access2007中使用日期类型查询数据
    朗道统计物理化学势的引入
    vue实现关键字查询列表数据
    grafana 通过查询结果设置动态阈值
  • 原文地址:https://blog.csdn.net/piaomiao_/article/details/138154080