-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflyweight.cpp
92 lines (77 loc) · 2.17 KB
/
flyweight.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
/*!
* \brief. Flyweight. Use sharing to support large numbers
* of fine-grained objects efficiently.
*
* FlyweightFactory <--> Flyweight
* | |
* ConcreteFlyweight UnsharedConcreteFlyweight
*/
#include <iostream>
#include <map>
class Flyweight {
public:
virtual void Operation() = 0;
};
class ConcreteFlyweight : public Flyweight {
public:
void Operation() {
std::cout << "ConcreteFlyweight's Operation." << std::endl;
}
};
class FlyweightFactory {
public:
void Initialize() {
obj_map_['A'] = new ConcreteFlyweight;
obj_map_['B'] = new ConcreteFlyweight;
}
void ClearFlyweight() {
std::map<char, Flyweight*>::iterator it = obj_map_.begin();
while (it != obj_map_.end()) {
std::cout << "Release object " << it->first << std::endl;
delete it->second;
obj_map_.erase(it++);
}
}
Flyweight* GetFlyweight(char tag) {
std::map<char, Flyweight*>::iterator it = obj_map_.find(tag);
if (it != obj_map_.end()) {
return (Flyweight*)it->second;
}
else {
std::cout << "Create new object for " << tag << std::endl;
Flyweight* new_obj = new ConcreteFlyweight;
obj_map_[tag] = new_obj;
return new_obj;
}
}
private:
// The key member for Flyweight Pattern.
std::map<char, Flyweight*> obj_map_;
};
int main() {
FlyweightFactory* factory = new FlyweightFactory;
factory->Initialize();
Flyweight* ch1 = factory->GetFlyweight('A');
if (ch1 != NULL)
ch1->Operation();
Flyweight* ch2 = factory->GetFlyweight('B');
if (ch2 != NULL)
ch2->Operation();
Flyweight* ch3 = factory->GetFlyweight('C');
if (ch3 != NULL)
ch3->Operation();
Flyweight* ch4 = factory->GetFlyweight('A');
if (ch4 != NULL)
ch4->Operation();
// The address of ch4 is the same as ch1.
std::cout << "address: " << ch1 << ", " << ch2 << ", " \
<< ch3 << ", " << ch4 << std::endl;
std::cout << std::endl;
factory->ClearFlyweight();
std::cout << std::endl;
Flyweight* ch5 = factory->GetFlyweight('C');
if (ch5 != NULL)
ch5->Operation();
std::cout << "address: " << ch5 << std::endl;
return 0;
}