赞
踩
创建型(5种) 工厂方法 抽象工厂模式 单例模式 建造者模式 原型模式
结构型(7种) 适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式
行为型(11种) 策略模式 模板方法模式 观察者模式 迭代器模式 责任链模式 命令模式
备忘录模式 状态模式 访问者模式 中介者模式
设计模式中的策略模式(Strategy Pattern)是一种行为设计模式,它定义了一系列算法,并将每个算法封装在一个单独的类中,使得它们可以互相替换。策略模式使得算法可以在运行时根据需要动态地改变,同时客户端代码可以通过统一的接口调用不同的策略实现。
策略接口(Strategy Interface):
- 定义所有支持的策略或算法所共有的方法签名,这是所有具体策略类的抽象父类或接口。
具体策略类(Concrete Strategies):
- 每个具体策略类实现了策略接口,并提供了算法的具体实现。
- 在具体策略类中包含了算法的详细逻辑。
上下文(Context):
- 上下文是使用策略的对象,它维持对策略对象的引用,并定义了如何使用策略的方法。
- 上下文可以根据需求改变策略,通常是通过策略接口设置具体的策略对象。
- 客户端:创建并配置上下文对象,指定要使用的具体策略。
- 上下文:根据客户端的配置,保存一个指向具体策略对象的引用,并在需要执行策略时调用策略接口定义的方法。
- 具体策略:执行实际的算法或行为。
- 开放封闭原则:策略模式允许在不修改现有代码的基础上新增策略。
- 多态性:客户端通过策略接口调用方法,无需关注具体实现细节,增强了代码的灵活性和可扩展性。
- 解耦:策略模式将算法从使用它的上下文中解耦出来,便于算法的独立管理和测试。
- 策略类数量增多:随着策略数量的增加,可能会产生大量的策略类。
- 上下文需了解策略:虽然上下文不用关心策略的具体实现,但是它至少需要知道有哪些策略可供选择,并能够适配不同的策略。
- 系统需要多种算法解决同一问题,且在运行时可以动态切换算法。
- 算法的实现可以相互独立,互不影响。
- 希望避免使用多重条件判断(例如 switch-case 或 if-else)来选择算法。
- // 抽象策略接口
- public interface DiscountStrategy {
- double getDiscount(double price);
- }
-
- // 具体策略类 - 无折扣策略
- public class NoDiscountStrategy implements DiscountStrategy {
- @Override
- public double getDiscount(double price) {
- return price;
- }
- }
-
- // 具体策略类 - 普通会员折扣策略
- public class NormalMemberDiscountStrategy implements DiscountStrategy {
- @Override
- public double getDiscount(double price) {
- return price * 0.95; // 九五折
- }
- }
-
- // 具体策略类 - VIP会员折扣策略
- public class VIPDiscountStrategy implements DiscountStrategy {
- @Override
- public double getDiscount(double price) {
- return price * 0.85; // 八五折
- }
- }
-
- // 上下文类 - 订单类,使用策略来计算折扣后的价格
- public class Order {
- private DiscountStrategy discountStrategy;
-
- public Order(DiscountStrategy discountStrategy) {
- this.discountStrategy = discountStrategy;
- }
-
- public void setDiscountStrategy(DiscountStrategy discountStrategy) {
- this.discountStrategy = discountStrategy;
- }
-
- public double calculateFinalPrice(double originalPrice) {
- return discountStrategy.getDiscount(originalPrice);
- }
- }
-
- // 客户端代码
- public class Client {
- public static void main(String[] args) {
- Order orderWithoutDiscount = new Order(new NoDiscountStrategy());
- System.out.println("No discount applied: " + orderWithoutDiscount.calculateFinalPrice(100));
-
- Order normalOrder = new Order(new NormalMemberDiscountStrategy());
- System.out.println("Normal member discount applied: " + normalOrder.calculateFinalPrice(100));
-
- Order vipOrder = new Order(new VIPDiscountStrategy());
- System.out.println("VIP member discount applied: " + vipOrder.calculateFinalPrice(100));
- }
- }

- # 策略接口
- from abc import ABC, abstractmethod
-
- class PaymentStrategy(ABC):
- @abstractmethod
- def pay(self, amount: float) -> None:
- pass
-
- # 具体策略类
- class CreditCardPayment(PaymentStrategy):
- def __init__(self, card_number: str, cvv: str):
- self.card_number = card_number
- self.cvv = cvv
-
- def pay(self, amount: float) -> None:
- print(f"Paid {amount} using credit card ({self.card_number})")
-
- class PayPalPayment(PaymentStrategy):
- def __init__(self, account_id: str):
- self.account_id = account_id
-
- def pay(self, amount: float) -> None:
- print(f"Paid {amount} using PayPal account ({self.account_id})")
-
- # 上下文
- class ShoppingCart:
- def __init__(self, payment_strategy: PaymentStrategy):
- self.payment_strategy = payment_strategy
-
- def set_payment_strategy(self, strategy: PaymentStrategy) -> None:
- self.payment_strategy = strategy
-
- def checkout(self, total_amount: float) -> None:
- print(f"Checking out with total amount: {total_amount}")
- self.payment_strategy.pay(total_amount)
-
- # 客户端代码
- cart = ShoppingCart(CreditCardPayment("1234567890123456", "123"))
- cart.checkout(100.00)
-
- cart.set_payment_strategy(PayPalPayment("buyer@example.com"))
- cart.checkout(200.00)

在这个示例中:
PaymentStrategy
是策略接口,定义了支付方法pay
。CreditCardPayment
和PayPalPayment
是具体策略类,实现了支付方式。ShoppingCart
是上下文,持有一个支付策略对象,并在其checkout
方法中调用策略对象的pay
方法来完成支付。根据需要,可以随时更改支付策略。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。