-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflyweight.h
More file actions
101 lines (95 loc) · 2.24 KB
/
flyweight.h
File metadata and controls
101 lines (95 loc) · 2.24 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
#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <initializer_list>
using namespace std;
struct sharedstate //ex> »óŰª
{
string brand;
string model;
string color;
sharedstate(const string& _brand, const string& _model, const string& _color)
:brand(_brand), model(_model), color(_color)
{
}
friend ostream& operator <<(ostream& os, const sharedstate& ss)
{
return os << "[" << ss.brand << " , " << ss.model << " , " << ss.color << " ]";
}
};
struct uniquestate
{
string owner;
string plates;
uniquestate(const string _owner, const string _plates)
:owner(_owner), plates(_plates)
{
}
friend ostream& operator <<(ostream& os, const uniquestate& ss)
{
return os << "[" << ss.owner << " , " << ss.plates << " ]";
}
};
class flyweight
{
private:
sharedstate* state;
public:
flyweight(const sharedstate* _state):state(new sharedstate(*_state))
{}
flyweight(const flyweight& other) :state(new sharedstate(*other.state))
{}
~flyweight()
{
delete state;
}
sharedstate* shared_state() const
{
return state;
}
void Operation(const uniquestate& _state) const
{
cout << "flyweight : displaying shared (" << _state << ") and unique (" << _state << ") state. \n";
}
};
class flyweight_factory
{
private:
unordered_map<string, flyweight> _fly;
string get_key(const sharedstate& ss) const
{
return ss.brand + "_" + ss.model + "_" + ss.color;
}
public:
flyweight_factory(std::initializer_list<sharedstate> _state)
{
for (const sharedstate& ss : _state)
{
this->_fly.insert(make_pair<string, flyweight>(this->get_key(ss), flyweight(&ss)));
}
}
flyweight getflyweight(const sharedstate& _state)
{
string key = this->get_key(_state);
if (this->_fly.find(key) == this->_fly.end())
{
cout << "flyweightfactory : can't find flyweight. please create onething." << endl;
this->_fly.insert(make_pair(key, flyweight(&_state)));
}
else
{
cout << "flyweightfactory : reusing existing flyweight." << endl;
}
return this->_fly.at(key);
}
void list_flyweight() const
{
size_t count = this->_fly.size();
cout << "\n flyweightfactory L i have " << count << "flyweights: " << endl;
for (pair<string, flyweight> _pair : this->_fly)
{
cout << _pair.first << endl;
}
}
};