9.1 预想方案

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
#include <iostream>
#include <sstream>
#include <string>

struct Shape
{
virtual std::string str() const = 0;
};

struct Circle : Shape
{
float radius;

explicit Circle(const float radius)
: radius{radius} {}

void resize(float factor) { radius *= factor; }

std::string str() const override
{
std::ostringstream oss;
oss << "A circle of radius " << radius;
return oss.str();
}
};

int main(void)
{
return 0;
}

9.2 动态装饰器

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
#include <iostream>
#include <sstream>
#include <string>
#include <cstdint>

struct Shape
{
virtual std::string str() const = 0;
};

struct Circle : Shape
{
float radius;

explicit Circle(const float radius)
: radius{radius} {}

void resize(float factor) { radius *= factor; }

std::string str() const override
{
std::ostringstream oss;
oss << "A circle of radius " << radius << std::endl;
return oss.str();
}
};

struct Square : Shape
{
float edge;

explicit Square(const float edge)
: edge{edge} {}

void resize(float factor) { edge *= factor; }

std::string str() const override
{
std::ostringstream oss;
oss << "A Square of edge " << edge << std::endl;
return oss.str();
}
};

struct ColoredShape : Shape
{
Shape& shape;
std::string color;

ColoredShape(Shape& shape, const std::string& color)
: shape{shape}, color{color} {}

std::string str() const override
{
std::ostringstream oss;
oss << shape.str() << " has the color " << color << std::endl;
return oss.str();
}
void make_dark();
};

void ColoredShape::make_dark() {
if (constexpr auto dark = "dark"; !color.starts_with(dark))
{
color.insert(0, dark);
}
}

struct TransparentShape : Shape
{
Shape& shape;
uint8_t transparentcy;

TransparentShape(Shape& shape, const uint8_t transparentcy)
: shape{shape}, transparentcy{transparentcy} {}

std::string str() const override
{
std::ostringstream oss;
oss << shape.str() << " has "
<< static_cast<float>(transparentcy) / 255.f * 100.f
<< "% transparentcy" << std::endl;

return oss.str();
}
};

int main(void)
{
Circle circle{0.5f};
ColoredShape redCircle{circle, "red"};
std::cout << redCircle.str();

redCircle.make_dark();
std::cout << redCircle.str();


Square square{3};
TransparentShape demiSquare{square, 85};
std::cout << demiSquare.str();

Circle c{23};
ColoredShape cs{c, "green"};
TransparentShape myCircle{cs, 64};
std::cout << myCircle.str();

return 0;
}

9.3 静态装饰器

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
#include <iostream>
#include <sstream>
#include <string>
#include <cstdint>
#include <type_traits>

struct Shape
{
virtual std::string str() const = 0;
};

struct Circle : Shape
{
float radius;

explicit Circle(const float radius)
: radius{radius} {}

void resize(float factor) { radius *= factor; }

std::string str() const override
{
std::ostringstream oss;
oss << "A circle of radius " << radius << std::endl;
return oss.str();
}
};

struct Square : Shape
{
float edge;

explicit Square(const float edge)
: edge{edge} {}

explicit Square()
: edge{0} {}

void resize(float factor) { edge *= factor; }

std::string str() const override
{
std::ostringstream oss;
oss << "A Square of edge " << edge << std::endl;
return oss.str();
}
};
/*
struct ColoredShape : Shape
{
Shape& shape;
std::string color;

ColoredShape(Shape& shape, const std::string& color)
: shape{shape}, color{color} {}

std::string str() const override
{
std::ostringstream oss;
oss << shape.str() << " has the color " << color << std::endl;
return oss.str();
}
void make_dark();
};

void ColoredShape::make_dark() {
if (constexpr auto dark = "dark"; !color.starts_with(dark))
{
color.insert(0, dark);
}
}
*/

template <typename T>
struct ColoredShape : T
{
static_assert(std::is_base_of_v<Shape, T>, "Template argument must be a Shape");

ColoredShape(const std::string& color)
: color{color} {}

ColoredShape()
: ColoredShape{} {};

std::string color;
std::string str() const override
{
std::ostringstream oss;
oss << T::str() << " has the color " << color << std::endl;
return oss.str();
}
};


/*
struct TransparentShape : Shape
{
Shape& shape;
uint8_t transparentcy;

TransparentShape(Shape& shape, const uint8_t transparentcy)
: shape{shape}, transparentcy{transparentcy} {}

std::string str() const override
{
std::ostringstream oss;
oss << shape.str() << " has "
<< static_cast<float>(transparentcy) / 255.f * 100.f
<< "% transparentcy" << std::endl;

return oss.str();
}
};
*/
template <typename T>
struct TransparentShape : T
{
static_assert(std::is_base_of_v<Shape, T>, "Template argument must be a Shape");

uint8_t transparentcy;

TransparentShape(const uint8_t transparentcy)
: transparentcy{transparentcy} {}

TransparentShape()
: transparentcy{} {};

std::string str() const override
{
std::ostringstream oss;
oss << T::str() << " has "
<< static_cast<float>(transparentcy) / 255.f * 100.f
<< "% transparentcy" << std::endl;

return oss.str();
}
};

int main(void)
{
/*
Circle circle{0.5f};
ColoredShape redCircle{circle, "red"};
std::cout << redCircle.str();

redCircle.make_dark();
std::cout << redCircle.str();


Square square{3};
TransparentShape demiSquare{square, 85};
std::cout << demiSquare.str();

Circle c{23};
ColoredShape cs{c, "green"};
TransparentShape myCircle{cs, 64};
std::cout << myCircle.str();
*/

ColoredShape<TransparentShape<Square>> square{"blue"};
// ColoredShape<Square> square{"blue"};
square.edge = 2;
square.transparentcy = 0.5;
std::cout << square.str();
return 0;
}

9.4 函数装饰器

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
#include <iostream>
#include <functional>

struct Logger
{
std::function<void()> func;
std::string name;

Logger(const std::function<void()>&func, const std::string& name)
: func{func}, name{name} {}

void operator()() const
{
std::cout << "Entering " << name << "\n";
func();
std::cout << "Exiting " << name << "\n";
}

};

int main(void)
{
Logger([]() { std::cout << "Hello\n"; }, "HelloFunction")();

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
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
#include <iostream>
#include <functional>
#include "stdarg.h"

struct Logger
{
std::function<void()> func;
std::string name;

Logger(const std::function<void()>&func, const std::string& name)
: func{func}, name{name} {}

void operator()() const
{
std::cout << "Entering " << name << "\n";
func();
std::cout << "Exiting " << name << "\n";
}

};

template <typename Func>
struct Logger2
{
Func func;
std::string name;

Logger2(const Func& func, const std::string& name)
: func{func}, name{name} {}

void operator()() const
{
std::cout << "Entering" << name << std::endl;
func();
std::cout << "Exiting" << name << std::endl;
}
};

template <typename Func> auto make_logger2(Func func, const std::string& name)
{
return Logger2<Func>{ func, name };
}

double add(double a, double b)
{
std::cout << a << "+" << b << "=" << (a + b) << std::endl;
return a+b;
}

template <typename R, typename... Args>
struct Logger3
{
Logger3(std::function<R(Args...)> func, const std::string& name)
: func{func}, name{name} {}

R operator() (Args ...args)
{
std::cout << "Entering " << name << std::endl;
R result = func(args...);
std::cout << "Exiting " << name << std::endl;
return result;
}

std::function<R(Args...)> func;
std::string name;
};

template <typename R, typename... Args>
auto make_logger3(R (*func)(Args...), const std::string& name)
{
return Logger3<R, Args...>(std::function<R(Args...)>(func), name);
}

int main(void)
{
Logger([]() { std::cout << "Hello\n"; }, "HelloFunction")();

auto call = make_logger2([]() { std::cout << "Hello!" << std::endl; },
"HelloFunction");
call();

auto logged_add = make_logger3(add, std::string("Add"));
auto result = logged_add(2, 3);

return 0;
}