-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShoppingCart.cpp
More file actions
126 lines (101 loc) · 3.28 KB
/
Copy pathShoppingCart.cpp
File metadata and controls
126 lines (101 loc) · 3.28 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ===========================================================================
// ShoppingCart.cpp // Concrete Strategy Pattern
// ===========================================================================
#include <memory>
#include <print>
#include <string>
namespace ConceptualExample_ShoppingCart {
class PricingStrategy {
public:
virtual ~PricingStrategy() = default;
[[nodiscard]]
virtual double price(double base) const = 0;
};
class NormalPrice : public PricingStrategy {
public:
[[nodiscard]]
double price(double base) const override {
return base;
}
};
class BlackFridayPrice : public PricingStrategy {
public:
[[nodiscard]]
double price(double base) const override {
return base * 0.7;
}
};
class ShoppingCart {
private:
const PricingStrategy& strategy;
public:
ShoppingCart(const PricingStrategy& strategy)
: strategy(strategy)
{}
[[nodiscard]]
double total(double base) const {
return strategy.price(base);
}
};
class Context
{
/**
* The Context maintains a reference to one of the Strategy objects.
* The Context does not know the concrete class of a strategy.
* It should work with all strategies via the 'Strategy' interface.
*/
private:
std::unique_ptr<PricingStrategy> m_strategy;
public:
Context(std::unique_ptr<PricingStrategy> strategy)
: m_strategy{ std::move(strategy) }
{}
~Context() = default;
void setStrategy(std::unique_ptr<PricingStrategy> strategy)
{
m_strategy = std::move(strategy);
}
/**
* The Context delegates some work to the Strategy object instead of
* implementing multiple versions of the algorithm on its own.
*/
void doShopping()
{
double moneyAvailable = 100.0;
std::println("moneyAvailable: {}", moneyAvailable);
double moneyToSpend = m_strategy->price(moneyAvailable);
std::println("moneyToSpend: {}", moneyToSpend);
}
};
/**
* The client code picks a concrete strategy and passes it to the context.
* The client should be aware of the differences between strategies
* in order to make the right choice.
*/
static void clientCode01()
{
BlackFridayPrice blackFriday;
ShoppingCart cart(blackFriday);
std::println("{}", cart.total(100)); // 70
}
static void clientCode02()
{
std::println("Client: Strategy is set to 'Normal Prices':");
Context context{ std::make_unique<NormalPrice>() };
context.doShopping();
std::println();
std::println("Client: Strategy is set to 'Black Friday':");
context.setStrategy(std::make_unique<BlackFridayPrice>());
context.doShopping();
std::println();
}
}
void test_shopping_cart()
{
using namespace ConceptualExample_ShoppingCart;
clientCode01();
clientCode02();
}
// ===========================================================================
// End-of-File
// ===========================================================================