享元模式

【1】什么是享元模式?

享元模式:

【2】享元模式的代码示例:

代码示例1:

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 using namespace std;
 5 
 6 class Flyweight
 7 {
 8 public:
 9     virtual void operation(int) = 0;
10 };
11 
12 class ConcreteFlyweight : public Flyweight
13 {
14     void operation(int extrinsicState)
15     {
16         cout << "具体FlyWeight: " << extrinsicState << endl;
17     }
18 };
19 
20 class UnsharedConcreteFlyweight : public Flyweight
21 {
22     void operation(int extrinsicState)
23     {
24         cout << "不共享的具体FlyWeight: " << extrinsicState << endl;        
25     }
26 };
27 
28 class FlyweightFactory
29 {
30 private:
31     map<string,Flyweight*> flyweights;
32 public:
33     FlyweightFactory()
34     {
35         flyweights["X"] = new ConcreteFlyweight();
36         flyweights["Y"] = new ConcreteFlyweight();
37         flyweights["Z"] = new UnsharedConcreteFlyweight();
38     }
39     Flyweight *getFlyweight(string key)
40     {
41         return (Flyweight *)flyweights[key];
42     }
43 };
44 
45 int main()
46 {
47     int state = 22;
48     FlyweightFactory *f = new FlyweightFactory();
49     
50     Flyweight *fx = f->getFlyweight("X");
51     fx->operation(--state);
52     
53     Flyweight *fy = f->getFlyweight("Y");
54     fy->operation(--state);
55 
56     Flyweight *fz = f->getFlyweight("Z");
57     fz->operation(--state);
58 
59     Flyweight *uf = new UnsharedConcreteFlyweight();
60     uf->operation(--state);
61 
62     return 0;
63 }
64 //Result:
65 /*
66 具体FlyWeight: 21
67 具体FlyWeight: 20
68 不共享的具体FlyWeight: 19
69 不共享的具体FlyWeight: 18
70 */
View Code

代码示例2:

 1 #include <iostream>
 2 #include <list>
 3 #include <string>
 4 #include <map>
 5 using namespace std;
 6 
 7 class WebSite
 8 {
 9 public:
10     virtual void use() = 0;
11 }; 
12 
13 class ConcreteWebSite : public WebSite
14 {
15 private:
16     string name;
17 public:
18     ConcreteWebSite(string name)
19     {
20         this->name = name;
21     }
22     void use()
23     {
24         cout << "网站分类: " << name << endl;
25     }
26 };
27 
28 class WebSiteFactory
29 {
30 private:
31     map<string,WebSite*> wf;
32 public:
33      
34     WebSite *getWebSiteCategory(string key)
35     {
36         if (wf.find(key) == wf.end())
37         {
38             wf[key] = new ConcreteWebSite(key);
39         }    
40         return wf[key];
41     }
42      
43     int getWebSiteCount()
44     {
45         return wf.size();
46     }
47 };
48 
49 int main()
50 {
51     WebSiteFactory *wf = new WebSiteFactory();
52 
53     WebSite *fx = wf->getWebSiteCategory("good");
54     fx->use();
55     
56      WebSite *fy = wf->getWebSiteCategory("产品展示");
57     fy->use();
58 
59     WebSite *fz = wf->getWebSiteCategory("产品展示");
60     fz->use();
61 
62 
63     WebSite *f1 = wf->getWebSiteCategory("博客");
64     f1->use();
65 
66     WebSite *f2 = wf->getWebSiteCategory("微博");
67     f2->use();
68 
69     cout << wf->getWebSiteCount() << endl;
70     return 0;
71 }
72 //Result:
73 /*
74 网站分类: good
75 网站分类: 产品展示
76 网站分类: 产品展示
77 网站分类: 博客
78 网站分类: 微博
79 4
80 */
View Code

 

Good   Good   Study, Day  Day  Up.

顺序  选择  循环  总结

posted @ 2014-09-04 21:54  kaizenly  阅读(250)  评论(0编辑  收藏  举报
打赏