Skip to content

Commit 85e2fe6

Browse files
author
jason_yao
committed
update
1 parent 9397bbd commit 85e2fe6

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

114 KB
Binary file not shown.

大話/cpp_pure/flyWeight/flyWeight.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ class WebSiteFactory {
4646

4747
private:
4848
bool initialized;
49+
#if 0
4950
void InitializeInstanceFields() {
5051
if (! initialized) {
5152
flyWeights = std::map<std::string, WebSite*>();
5253
initialized = true;
5354
}
5455
}
56+
#endif
5557

5658
public:
5759
WebSiteFactory() {
58-
InitializeInstanceFields();
60+
//InitializeInstanceFields();
5961
}
6062
};
6163

@@ -77,15 +79,15 @@ void ConcreteWebSite::setName(std::string name) {
7779

7880
WebSite* WebSiteFactory::getWebSiteCategory(std::string key) {
7981
// if (!flyWeights.containsKey(key)) {
80-
if (flyWeights.find(key) != flyWeights.end()) {
81-
flyWeights.insert(key, new ConcreteWebSite(key));
82+
if (flyWeights.find(key) == flyWeights.end()) {
83+
flyWeights.insert(std::pair<std::string, WebSite*>(key, new ConcreteWebSite(key)));
8284
}
8385

84-
return flyWeights->get(key);
86+
return flyWeights.find(key)->second;
8587
}
8688

8789
int WebSiteFactory::getWebSiteCount() {
88-
return flyWeights->size();
90+
return flyWeights.size();
8991
}
9092

9193
User::User(std::string name) {
@@ -112,6 +114,7 @@ int main(void) {
112114
wb4->use(new User("Lincoln"));
113115
WebSite* wb5 = wf->getWebSiteCategory("Group");
114116
wb5->use(new User("Kenny"));
115-
puts(wf->getWebSiteCount());
117+
//puts(wf->getWebSiteCount());
116118
return 0;
117119
}
120+

0 commit comments

Comments
 (0)