-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample03.cpp
More file actions
152 lines (125 loc) · 3.71 KB
/
ConceptualExample03.cpp
File metadata and controls
152 lines (125 loc) · 3.71 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// ===========================================================================
// ConceptualExample03.cpp // Factory Method // Real-World Example
// ===========================================================================
#include <string>
#include <memory>
#include <print>
namespace ConceptualExample03
{
/**
* Product Interface
*/
class HotDrink
{
public:
virtual ~HotDrink() {}
virtual void prepare(int volume) = 0;
virtual void drink() = 0;
virtual std::string name() = 0;
};
/**
* Concrete Products provide various implementations of the Product interface.
*/
class Tea : public HotDrink
{
void prepare(int volume) override
{
std::print("Take tea bag, boil water, pour {} ml, add some lemon", volume);
}
void drink() override
{
std::print("Hmmm, what a delicious tea :)");
}
std::string name () override
{
return "Tea";
}
};
class Coffee : public HotDrink
{
void prepare(int volume) override
{
std::print("Coffee {} ml, add some milk and sugar", volume);
}
void drink() override
{
std::print("Hmmm, what a delicious coffee :)");
}
std::string name() override
{
return "Coffee";
}
};
/**
* More hypothetical make_drink() function that would take the name of a drink
* and make that drink. Given a discrete set of cases, this can look rather tedious:
*/
static std::unique_ptr<HotDrink> makeDrink(const std::string& type)
{
std::unique_ptr<HotDrink> drink;
if (type == "tea")
{
drink = std::make_unique<Tea>();
drink->prepare(200);
}
else
{
drink = std::make_unique<Coffee>();
drink->prepare(50);
}
return drink;
}
/**
* The FactoryBase class declares the factory method
* that is supposed to return an object of a Product class.
* The FactoryBase's subclasses usually provide the implementation of this method.
*/
class HotDrinkFactory
{
public:
virtual ~HotDrinkFactory() {}
virtual std::unique_ptr<HotDrink> makeDrink() const = 0;
};
/**
* Concrete HotDrinkFactory classes override the factory method 'makeDrink'
* in order to change the resulting product's type.
*/
class CoffeeFactory : public HotDrinkFactory
{
std::unique_ptr<HotDrink> makeDrink() const override
{
return std::make_unique<Coffee>();
}
};
class TeaFactory : public HotDrinkFactory
{
std::unique_ptr<HotDrink> makeDrink() const override
{
return std::make_unique<Tea>();
}
};
static void clientCode(const HotDrinkFactory& factory) {
std::println("Client: Not aware of the concrete creator's class (HotDrinkFactory):");
std::unique_ptr<HotDrink> beverage{ factory.makeDrink() };
std::println("Created {}", beverage->name());
beverage->drink();
}
}
/**
* The Application picks a factory's type
* depending on the configuration or environment.
*/
void test_conceptual_example_03()
{
using namespace ConceptualExample03;
std::println("Example: Launched with CoffeeFactory:");
CoffeeFactory coffeeFactory{};
clientCode(coffeeFactory);
std::println();
TeaFactory teaFactory{};
clientCode(teaFactory);
std::println();
}
// ===========================================================================
// End-of-File
// ===========================================================================