享元模式

【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 @   kaizenly  阅读(253)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2013-09-04 资源共享型智能指针实现方式
打赏

喜欢请打赏

扫描二维码打赏

微信打赏

点击右上角即可分享
微信分享提示