• 设计模式——策略模式


    定义:


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

    主要角色如下:

    示例:

    假定现在需要实现一个简化的报价管理,实现如下的功能

    −对普通客户或者是新客户报全价

    −对老客户报的价格,统一折扣5%

    −对大客户报的价格,统一折扣10%

    代码:

    1. // 定义策略接口
    2. interface PricingStrategy {
    3. double calculatePrice(double originalPrice);
    4. }
    5. // 实现不同报价策略的具体类
    6. class NormalPricingStrategy implements PricingStrategy {
    7. @Override
    8. public double calculatePrice(double originalPrice) {
    9. return originalPrice; // 普通客户或新客户报全价
    10. }
    11. }
    12. class OldCustomerPricingStrategy implements PricingStrategy {
    13. @Override
    14. public double calculatePrice(double originalPrice) {
    15. return originalPrice * 0.95; // 对老客户报的价格,统一折扣5%
    16. }
    17. }
    18. class BigCustomerPricingStrategy implements PricingStrategy {
    19. @Override
    20. public double calculatePrice(double originalPrice) {
    21. return originalPrice * 0.9; // 对大客户报的价格,统一折扣10%
    22. }
    23. }
    24. // 上下文类
    25. class QuoteManager {
    26. private PricingStrategy pricingStrategy;
    27. public void setPricingStrategy(PricingStrategy pricingStrategy) {
    28. this.pricingStrategy = pricingStrategy;
    29. }
    30. public double generateQuote(double originalPrice) {
    31. return pricingStrategy.calculatePrice(originalPrice);
    32. }
    33. }
    34. // 测试
    35. public class PricingStrategyExample {
    36. public static void main(String[] args) {
    37. QuoteManager quoteManager = new QuoteManager();
    38. // 普通客户或新客户
    39. quoteManager.setPricingStrategy(new NormalPricingStrategy());
    40. double normalQuote = quoteManager.generateQuote(100.0);
    41. System.out.println("普通客户报价: " + normalQuote);
    42. // 老客户
    43. quoteManager.setPricingStrategy(new OldCustomerPricingStrategy());
    44. double oldCustomerQuote = quoteManager.generateQuote(100.0);
    45. System.out.println("老客户报价: " + oldCustomerQuote);
    46. // 大客户
    47. quoteManager.setPricingStrategy(new BigCustomerPricingStrategy());
    48. double bigCustomerQuote = quoteManager.generateQuote(100.0);
    49. System.out.println("大客户报价: " + bigCustomerQuote);
    50. }
    51. }

     运行截图:

    分析:

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

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

    2. 具体报价策略类

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

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

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

  • 相关阅读:
    地球系统模式(CESM)实践技术
    深入理解数据库视图
    攻防千层饼
    【小黑送书—第七期】>>程序员To架构师?一起来看架构师系列书籍推荐
    基于JAVA中华二十四节气文化传承宣展平台计算机毕业设计源码+系统+数据库+lw文档+部署
    mySQL中查询统计俩个表相加COUNT之合
    Python 爬虫实战之爬拼多多商品并做数据分析
    小H靶场学习笔记:DC-2
    L - Intersection and Union Gym - 103993L
    pytest---添加自定义命令行参数(pytest_addoption )
  • 原文地址:https://blog.csdn.net/q12ERTYU/article/details/136221684