-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPaintBrush.cpp
More file actions
138 lines (107 loc) · 3.75 KB
/
PaintBrush.cpp
File metadata and controls
138 lines (107 loc) · 3.75 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
// ===========================================================================
// PaintBrush.cpp // Flyweight Pattern
// ===========================================================================
#include <iostream>
#include <unordered_map>
#include <memory>
namespace PaintBrushFlyweight {
class Pen
{
public:
~Pen() {}
virtual void setColor(const std::string& color) = 0;
virtual void draw(const std::string& content) = 0;
virtual void print() = 0;
};
enum class BrushSize { Thin, Medium, Thick };
class ThickPen : public Pen
{
private:
const BrushSize m_brushSize; // intrinsic state - shareable
std::string m_color; // extrinsic state - supplied by client
public:
ThickPen() : m_brushSize{ BrushSize::Thick } {}
void setColor(const std::string& color) override {
m_color = color;
}
void draw(const std::string& content) override {
std::cout
<< "Drawing THICK content in color : " << m_color
<< " - " << content << std::endl;
}
void print() override {
std::cout << this << std::endl;
}
};
class ThinPen : public Pen
{
private:
const BrushSize m_brushSize; // intrinsic state - shareable
std::string m_color; // extrinsic state - supplied by client
public:
ThinPen() : m_brushSize{ BrushSize::Thin } {}
void setColor(const std::string& color) override {
m_color = color;
}
void draw(const std::string& content) override {
std::cout
<< "Drawing THIN content in color : " << m_color
<< " - " << content << std::endl;
}
void print() override {
std::cout << this << std::endl;
}
};
class PenFactory
{
private:
static std::unordered_map<std::string, std::shared_ptr<Pen>> pensMap;
public:
static std::shared_ptr<Pen> getThickPen(const std::string& color)
{
std::string key{ color + "-THICK" };
std::shared_ptr<Pen> pen{ pensMap[key] };
if (pen != nullptr) {
return pen;
}
else {
pen = std::make_shared<ThickPen>();
pen->setColor(color);
pensMap[key] = pen;
}
return pen;
}
static std::shared_ptr<Pen> getThinPen(const std::string& color)
{
std::string key{ color + "-THIN" };
std::shared_ptr<Pen> pen{ pensMap[key] };
if (pen != nullptr) {
return pen;
}
else {
pen = std::make_shared<ThickPen>();
pen->setColor(color);
pensMap[key] = pen;
}
return pen;
}
};
// out-of-class definition of static class member
std::unordered_map<std::string, std::shared_ptr<Pen>> PenFactory::pensMap;
}
void test_paint_brush()
{
using namespace PaintBrushFlyweight;
std::shared_ptr<Pen> yellowThinPen1{ PenFactory::getThickPen("YELLOW") }; // creating new pen
yellowThinPen1->draw("Hello World !!");
std::shared_ptr<Pen> yellowThinPen2{ PenFactory::getThickPen("YELLOW") }; // pen is shared
yellowThinPen2->draw("Hello World !!");
std::shared_ptr<Pen> blueThinPen{ PenFactory::getThickPen("BLUE") }; // creating new pen
blueThinPen->draw("Hello World !!");
yellowThinPen1->print();
yellowThinPen2->print();
blueThinPen->print();
}
// ===========================================================================
// End-of-File
// ===========================================================================