设计模式——策略模式

定义:

该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。策略模式属于对象行为模式,它通过对算法进行封装,把便用算法的责任和算法的实现分割开来,并委派给不算法进象行管理。

主要角色如下:

示例:

假定现在需要实现一个简化的报价管理,实现如下的功能
−对普通客户或者是新客户报全价
−对老客户报的价格,统一折扣5%
−对大客户报的价格,统一折扣10%

代码:

java 复制代码
// 定义策略接口
interface PricingStrategy {
    double calculatePrice(double originalPrice);
}

// 实现不同报价策略的具体类
class NormalPricingStrategy implements PricingStrategy {
    @Override
    public double calculatePrice(double originalPrice) {
        return originalPrice; // 普通客户或新客户报全价
    }
}

class OldCustomerPricingStrategy implements PricingStrategy {
    @Override
    public double calculatePrice(double originalPrice) {
        return originalPrice * 0.95; // 对老客户报的价格,统一折扣5%
    }
}

class BigCustomerPricingStrategy implements PricingStrategy {
    @Override
    public double calculatePrice(double originalPrice) {
        return originalPrice * 0.9; // 对大客户报的价格,统一折扣10%
    }
}

// 上下文类
class QuoteManager {
    private PricingStrategy pricingStrategy;

    public void setPricingStrategy(PricingStrategy pricingStrategy) {
        this.pricingStrategy = pricingStrategy;
    }

    public double generateQuote(double originalPrice) {
        return pricingStrategy.calculatePrice(originalPrice);
    }
}

// 测试
public class PricingStrategyExample {
    public static void main(String[] args) {
        QuoteManager quoteManager = new QuoteManager();

        // 普通客户或新客户
        quoteManager.setPricingStrategy(new NormalPricingStrategy());
        double normalQuote = quoteManager.generateQuote(100.0);
        System.out.println("普通客户报价: " + normalQuote);

        // 老客户
        quoteManager.setPricingStrategy(new OldCustomerPricingStrategy());
        double oldCustomerQuote = quoteManager.generateQuote(100.0);
        System.out.println("老客户报价: " + oldCustomerQuote);

        // 大客户
        quoteManager.setPricingStrategy(new BigCustomerPricingStrategy());
        double bigCustomerQuote = quoteManager.generateQuote(100.0);
        System.out.println("大客户报价: " + bigCustomerQuote);
    }
}

运行截图:

分析:

以上代码实现了一个报价管理系统,使用了策略模式。下面是代码的解析:

  1. PricingStrategy 接口 :定义了计算最终报价的策略接口,其中包含了一个 calculatePrice 方法用于计算最终的报价。

  2. 具体报价策略类

    • NormalPricingStrategy:普通客户或新客户的报价策略,直接返回原价。
    • OldCustomerPricingStrategy:老客户的报价策略,给予统一的5%折扣。
    • BigCustomerPricingStrategy:大客户的报价策略,给予统一的10%折扣。
  3. QuoteManager 类 :报价管理器类,负责接受不同的报价策略,并生成最终的报价。它包含了一个 setPricingStrategy 方法,用于设置报价策略,以及一个 generateQuote 方法,用于根据所选的策略生成最终的报价。

  4. 测试类 PricingStrategyExample:在这个类中进行了测试。首先创建了一个 QuoteManager 实例,然后设置不同的报价策略,并生成相应的报价,最后输出结果。

在测试中,我们对普通客户、老客户和大客户分别使用了不同的报价策略,得到了相应的报价。

相关推荐
碎梦归途1 小时前
23种设计模式-行为型模式之中介者模式(Java版本)
java·jvm·设计模式·中介者模式·软件设计师
不当菜虚困4 小时前
JAVA设计模式——(九)工厂模式
java·开发语言·设计模式
柴郡猫乐园4 小时前
智能指针之设计模式5
开发语言·设计模式·智能指针
mooridy8 小时前
设计模式 | 详解常用设计模式(六大设计原则,单例模式,工厂模式,建造者模式,代理模式)
c++·设计模式
程序员JerrySUN8 小时前
设计模式每日硬核训练 Day 17:中介者模式(Mediator Pattern)完整讲解与实战应用
microsoft·设计模式·中介者模式
Auroral15611 小时前
结构型模式:装饰器模式
设计模式
智想天开11 小时前
14.外观模式:思考与解读
windows·microsoft·设计模式·外观模式
摘星编程13 小时前
并发设计模式实战系列(9):消息传递(Message Passing)
设计模式·并发编程
此木|西贝21 小时前
【设计模式】享元模式
java·设计模式·享元模式
麓殇⊙1 天前
设计模式--建造者模式详解
设计模式·建造者模式