1 #include <iostream>
2 #include <map>
3 #include <string>
4 #include <cstring>
5 #include <string.h>
6 #include <cstdlib>
7 using namespace std;
8
9 class Book {
10 public:
11 string GetPublish() {
12 return *m_publishCompany;
13 }
14 string GetWriter() {
15 return *m_writer;
16 }
17 int GetBookID() {
18 return m_bookID;
19 }
20 int GetPrice() {
21 return m_price;
22 }
23 string GetName() {
24 return m_name;
25 }
26
27 void SetPublish(string *s) {
28 m_publishCompany = s;
29 }
30 void SetWriter(string *s) {
31 m_writer = s;
32 }
33 void SetBookID(int id) {
34 m_bookID = id;
35 }
36 void SetPrice(int price) {
37 m_price = price;
38 }
39 void SetName(string s) {
40 m_name = s;
41 }
42
43 private:
44 string *m_publishCompany; // 出版社
45 string *m_writer; // 作者
46 int m_bookID; // 书籍编号
47 int m_price; // 价钱
48 string m_name; // 书名
49 };
50
51 class PublishFlyweight {
52 public:
53 PublishFlyweight(string s) {
54 m_name = s;
55 }
56 string* GetName() {
57 return &m_name;
58 }
59
60 private:
61 string m_name;
62 };
63
64 class PublishFlyweightFactory {
65 public:
66 PublishFlyweight* GetPublish(string key) {
67 PublishFlyweight *p;
68 map<string, PublishFlyweight*>::iterator it;
69 it = mapPublish.find(key);
70
71 // 存在这个出版社
72 if (it != mapPublish.end()) {
73 // 这里可能有点难懂,请查阅STL的帮助文档
74 // 其实second就是指 map<string, PublishFlyweight*> 的 PublishFlyweight*
75 p = (*it).second;
76 cout << "已经有这个出版社: " << *(p->GetName()) << " 你节省了" << strlen((*(p->GetName())).c_str()) << "字节的空间" << endl;
77 }
78
79 else { // 插入这个PublishFlyweight
80
81 p = new PublishFlyweight(key);
82 mapPublish[key] = p;
83 }
84
85 return p;
86 }
87
88 private:
89 map<string, PublishFlyweight*> mapPublish;
90 };
91
92 class WriterFlyweight {
93 public:
94 WriterFlyweight(string s) {
95 m_name = s;
96 }
97 string* GetName() {
98 return &m_name;
99 }
100
101 private:
102 string m_name;
103 };
104
105 class WriterFlyweightFactory {
106 public:
107 WriterFlyweight* GetWriter(string key) {
108 WriterFlyweight *p;
109 map<string, WriterFlyweight*>::iterator it;
110 it = mapWriter.find(key);
111
112 // 存在这个Writer
113 if (it != mapWriter.end()) {
114 // 这里可能有点难懂,请查阅STL的帮助文档
115 // 其实second就是指 map<string, WriterFlyweight*> 的 WriterFlyweight*
116 p = (*it).second;
117 cout << "已经有这个作者名字: " << *(p->GetName()) << " 你节省了"
118 << strlen((*(p->GetName())).c_str()) << "字节的空间" << endl;
119 }
120
121 else { // 插入这个PublishFlyweight
122
123 p = new WriterFlyweight(key);
124 mapWriter[key] = p;
125 }
126
127 return p;
128 }
129
130 private:
131 map<string, WriterFlyweight*> mapWriter;
132 };
133
134 void ShowBookInfo(Book book) {
135 cout << "书名:" << book.GetName() << endl;
136 cout << "编号:" << book.GetBookID() << endl;
137 cout << "价钱:" << book.GetPrice() << endl;
138 cout << "出版:" << book.GetPublish() << endl;
139 cout << "作者:" << book.GetWriter() << endl;
140 cout << endl;
141 }
142
143 int main() {
144 PublishFlyweightFactory pff;
145 WriterFlyweightFactory wff;
146 Book book1, book2, book3;
147
148 book1.SetPublish(pff.GetPublish("机械工业出版社")->GetName());
149 book1.SetWriter((wff.GetWriter("候捷")->GetName()));
150 book1.SetBookID(0000);
151 book1.SetPrice(20);
152 book1.SetName(string("<<C++好野>>"));
153
154 ShowBookInfo(book1);
155
156 book2.SetPublish((pff.GetPublish("人民邮电出版社")->GetName()));
157 book2.SetWriter((wff.GetWriter("候捷")->GetName()));
158 book2.SetBookID(0001);
159 book2.SetPrice(30);
160 book2.SetName(string("<<C++是好劲>>"));
161
162 ShowBookInfo(book2);
163
164 book3.SetPublish((pff.GetPublish("机械工业出版社")->GetName()));
165 book3.SetWriter((wff.GetWriter("一雨田")->GetName()));
166 book3.SetBookID(0002);
167 book3.SetPrice(50);
168 book3.SetName(string("<<C++无得顶,我是铁头功...>>"));
169
170 ShowBookInfo(book3);
171 return 0;
172 }