概述
完成一项任务或事情,往往会有多种方法,而每一个方法可以称之为一个策略。而策略的选择,往往需要根据实际的环境或者条件的不同而做出不同的选择。
定义
定义一系列算法,将每一个算法封装起来,并让它们可以相互代替。
策略模式让算法独立于使用它的客户而变化。
角色
Context:
环境类Strategy:
抽象策略类ConcreteStrategy:
具体策略类
场景
在软件系统中,有许多算法可以实现某一功能,如查找、排序等,一种常用的方法是硬编码(
Hard Coding
)在一个类中,如需要提供多种查找算法,可以将这些算法写到一个类中,在该类中提供多个方法,每一个方法对应一个具体的查找算法;当然也可以将这些查找算法封装在一个统一的方法中,通过if…else…
等条件判断语句来进行选择。这两种实现方法我们都可以称之为硬编码。但是,如果需要增加一种新的查找算法,需要修改封装算法类的源代码;更换查找算法,也需要修改客户端调用代码。
这样就导致这个算法类中封装了大量查找算法,该类代码将较复杂,维护较为困难。
实现一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
//抽象策略类 class Strategy { public: virtual ~Strategy() {} virtual void algorithmInterface() = 0; }; //具体策略类-A class ConcreteStrategyA : public Strategy { public: ~ConcreteStrategyA() { /*...*/} void algorithmInterface() { std::cout << "Concrete Strategy A" << std::endl; } //... }; //具体策略类-B class ConcreteStrategyB : public Strategy { public: ~ConcreteStrategyB() { /*...*/} void algorithmInterface() { std::cout << "Concrete Strategy B" << std::endl; } //... }; //具体策略类-C class ConcreteStrategyA : public Strategy { public: ~ConcreteStrategyC() { /*...*/} void algorithmInterface() { std::cout << "Concrete Strategy C" << std::endl; } //... }; //环境类 class Context { public: Context(Strategy * const s):strategy(s) { } ~ Context() { delete strategy; } void contextInterface() { strategy->algorithmInterface(); } //... private: Strategy * strategy; //... }; |
1 2 3 4 5 6 |
auto main()->int { Context context(new ConcreteStrategyA()); context.contextInterface(); return 0; } |
实现二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// 策略接口 class DiscountStrategy { public: virtual double apply_discount(double price) = 0; }; // 具体策略类 class NoDiscount : public DiscountStrategy { public: double apply_discount(double price) override { return price; } }; class PercentageDiscount : public DiscountStrategy { public: PercentageDiscount(double percentage) : percentage(percentage) {} double apply_discount(double price) override { return price * (1 - percentage / 100); } private: double percentage; }; class FixedAmountDiscount : public DiscountStrategy { public: FixedAmountDiscount(double amount) : amount(amount) {} double apply_discount(double price) override { return price - amount; } private: double amount; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/ 主体对象 class Product { public: Product(const std::string& name, double price, DiscountStrategy* discount_strategy) : name(name), price(price), discount_strategy(discount_strategy) {} double get_discounted_price() { return discount_strategy->apply_discount(price); } private: std::string name; double price; DiscountStrategy* discount_strategy; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
int main() { DiscountStrategy* noDiscount = new NoDiscount(); DiscountStrategy* percentageDiscount = new PercentageDiscount(10); DiscountStrategy* fixedAmountDiscount = new FixedAmountDiscount(50); Product product1("Laptop", 1000, noDiscount); Product product2("Phone", 500, percentageDiscount); Product product3("Tablet", 300, fixedAmountDiscount); std::cout << product1.get_discounted_price() << std::endl; std::cout << product2.get_discounted_price() << std::endl; std::cout << product3.get_discounted_price() << std::endl; delete noDiscount; delete percentageDiscount; delete fixedAmountDiscount; return 0; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 结构型:桥接模式09/24
- ♥ 创建型:抽象工厂模式08/26
- ♥ 创建型:原型模式09/25
- ♥ 行为型:命令模式09/19
- ♥ 行为型:备忘录模式09/25
- ♥ 设计模式:归类04/18