-
Notifications
You must be signed in to change notification settings - Fork 80
/
G35_Decorator_1.cpp
210 lines (160 loc) · 4.94 KB
/
G35_Decorator_1.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**************************************************************************************************
*
* \file G35_Decorator_1.cpp
* \brief Guideline 35: Use Decorators to Add Customization Hierarchically
*
* Copyright (C) 2022 Klaus Iglberger - All Rights Reserved
*
* This file is part of the supplemental material for the O'Reilly book "C++ Software Design"
* (https://www.oreilly.com/library/view/c-software-design/9781098113155/).
*
**************************************************************************************************/
//---- <Money.h> --------------------------------------------------------------------------------
#include <cmath>
#include <concepts>
#include <cstdint>
#include <ostream>
struct Money
{
uint64_t value{};
};
template< typename T >
requires std::is_arithmetic_v<T>
Money operator*( Money money, T factor )
{
return Money{ static_cast<uint64_t>( money.value * factor ) };
}
constexpr Money operator+( Money lhs, Money rhs ) noexcept
{
return Money{ lhs.value + rhs.value };
}
std::ostream& operator<<( std::ostream& os, Money money )
{
return os << money.value;
}
//---- <Item.h> -----------------------------------------------------------------------------------
//#include <Money.h>
class Item
{
public:
virtual ~Item() = default;
virtual Money price() const = 0;
};
//---- <DecoratedItem.h> --------------------------------------------------------------------------
//#include <Item.h>
#include <memory>
#include <stdexcept>
#include <utility>
class DecoratedItem : public Item
{
public:
explicit DecoratedItem( std::unique_ptr<Item> item )
: item_( std::move(item) )
{
if( !item_ ) {
throw std::invalid_argument( "Invalid item" );
}
}
protected:
Item& item() { return *item_; }
Item const& item() const { return *item_; }
private:
std::unique_ptr<Item> item_;
};
//---- <CppBook.h> --------------------------------------------------------------------------------
//#include <Item.h>
#include <string>
#include <utility>
class CppBook : public Item
{
public:
CppBook( std::string title, Money price )
: title_{ std::move(title) }
, price_{ price }
{}
std::string const& title() const { return title_; }
Money price() const override { return price_; }
private:
std::string title_{};
Money price_{};
};
//---- <ConferenceTicket.h> -----------------------------------------------------------------------
//#include <Item.h>
#include <string>
#include <utility>
class ConferenceTicket : public Item
{
public:
ConferenceTicket( std::string name, Money price )
: name_{ std::move(name) }
, price_{ price }
{}
std::string const& name() const { return name_; }
Money price() const override { return price_; }
private:
std::string name_{};
Money price_{};
};
//---- <Discounted.h> -----------------------------------------------------------------------------
//#include <DecoratedItem.h>
class Discounted : public DecoratedItem
{
public:
Discounted( double discount, std::unique_ptr<Item> item )
: DecoratedItem( std::move(item) )
, factor_( 1.0 - discount )
{
if( !std::isfinite(discount) || discount < 0.0 || discount > 1.0 ) {
throw std::invalid_argument( "Invalid discount" );
}
}
Money price() const override
{
return item().price() * factor_;
}
private:
double factor_;
};
//---- <Taxed.h> ----------------------------------------------------------------------------------
//#include <DecoratedItem.h>
class Taxed : public DecoratedItem
{
public:
Taxed( double taxRate, std::unique_ptr<Item> item )
: DecoratedItem( std::move(item) )
, factor_( 1.0 + taxRate )
{
if( !std::isfinite(taxRate) || taxRate < 0.0 ) {
throw std::invalid_argument( "Invalid tax" );
}
}
Money price() const override
{
return item().price() * factor_;
}
private:
double factor_;
};
//---- <Main.cpp> ---------------------------------------------------------------------------------
//#include <ConferenceTicket.h>
//#include <CppBook.h>
//#include <Discounted.h>
//#include <Taxed.h>
#include <cstdlib>
#include <memory>
int main()
{
// 7% tax: 19*1.07 = 20.33
std::unique_ptr<Item> item1(
std::make_unique<Taxed>( 0.07,
std::make_unique<CppBook>( "Effective C++", Money{19} ) ) );
// 20% discount, 19% tax: (999*0.8)*1.19 = 951.05
std::unique_ptr<Item> item2(
std::make_unique<Taxed>( 0.19,
std::make_unique<Discounted>( 0.2,
std::make_unique<ConferenceTicket>( "CppCon", Money{999} ) ) ) );
Money const totalPrice1 = item1->price(); // Results in 20.33
Money const totalPrice2 = item2->price(); // Results in 951.05
// ...
return EXIT_SUCCESS;
}