简述:
享元模式:运用共享技术有效地支持大量细粒度的对象。
享元模式包括:享元工厂类、享元接口类、具体享元类
享元工厂类: 用来创建并管理享元接口类对象,它主要使用来确保合理地共享享元接口类,当用户请求一个享元接口类对象时,享元工厂类对象提供一个已创建的实例或者创建一个(不存在的话)
享元接口类:所有具体享元类的超类或接口,通过这个接口可以接受并作用于外部状态。
具体享元类:指那些不需要共享的享元接口类的子类,因为享元接口类共享成为可能,但它并不强制共享。
享元模式:
1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 // 享元接口类 6 class CFlyweight 7 { 8 public: 9 virtual void Operation(int nExtrinsicstate) = 0; 10 }; 11 12 // 具体享元类 13 class CConcreteFlyweight : public CFlyweight 14 { 15 public: 16 virtual void Operation(int nExtrinsicstate) { cout << "具体Flyweight:" << nExtrinsicstate << endl; } 17 }; 18 19 // 不需要共享的享元子类 20 class CUnsharedConcreteFlyWeight : public CFlyweight 21 { 22 public: 23 virtual void Operation(int nExtrinsicstate) { cout << "不共享的具体Flyweight:" << nExtrinsicstate << endl; } 24 }; 25 26 // 享元工厂类 27 class CFlyweightFactory 28 { 29 private: 30 map<string, CFlyweight*> m_mapFlyweights; 31 32 public: 33 CFlyweightFactory() 34 { 35 m_mapFlyweights.insert(make_pair("X", new CConcreteFlyweight())); 36 m_mapFlyweights.insert(make_pair("Y", new CConcreteFlyweight())); 37 m_mapFlyweights.insert(make_pair("Z", new CConcreteFlyweight())); 38 } 39 40 ~CFlyweightFactory() 41 { 42 map<string, CFlyweight*>::iterator ite = m_mapFlyweights.begin(); 43 while (ite != m_mapFlyweights.end()) 44 { 45 if((*ite).second) 46 { 47 delete (*ite).second; 48 (*ite).second = NULL; 49 } 50 51 ++ite; 52 } 53 54 m_mapFlyweights.clear(); 55 } 56 57 CFlyweight* GetFlyweight(string szKey) { return m_mapFlyweights[szKey]; } 58 }; 59 60 61 int main() 62 { 63 int nExtrinsicstate = 22; 64 65 CFlyweightFactory FlyweightFactory; 66 67 CFlyweight* fx = FlyweightFactory.GetFlyweight("X"); 68 fx->Operation(--nExtrinsicstate); 69 70 CFlyweight* fy = FlyweightFactory.GetFlyweight("Y"); 71 fy->Operation(--nExtrinsicstate); 72 73 CFlyweight* fz = FlyweightFactory.GetFlyweight("Z"); 74 fz->Operation(--nExtrinsicstate); 75 76 CUnsharedConcreteFlyWeight uf; 77 uf.Operation(--nExtrinsicstate); 78 79 system("pause"); 80 return 0; 81 }
输出结果:
例:网站共享代码
代码如下:
1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 // 用户类 6 class CUser 7 { 8 private: 9 string m_szName; 10 11 public: 12 CUser(string szName) { m_szName = szName; } 13 14 string GetName() { return m_szName; } 15 }; 16 17 // 网站基类(享元接口类) 18 class CWebSite 19 { 20 public: 21 virtual void Use(CUser* pUser) = 0; 22 }; 23 24 // 具体网站类(具体享元类) 25 class CConcreteWebSite : public CWebSite 26 { 27 private: 28 string m_szName; 29 30 public: 31 CConcreteWebSite(string szName) { m_szName = szName; } 32 33 virtual void Use(CUser* pUser) { cout << "网站分类: " + m_szName << " 用户: " + pUser->GetName() << endl; } 34 }; 35 36 // 网站工厂类(享元工厂类) 37 class CWebSiteFactory 38 { 39 private: 40 map<string, CWebSite*> m_mapWebSites; 41 42 public: 43 ~CWebSiteFactory() 44 { 45 map<string, CWebSite*>::iterator ite = m_mapWebSites.begin(); 46 while (ite != m_mapWebSites.end()) 47 { 48 if ((*ite).second) 49 { 50 delete (*ite).second; 51 (*ite).second = NULL; 52 } 53 ++ite; 54 } 55 m_mapWebSites.clear(); 56 } 57 58 // 获得网站分类 59 CWebSite* GetWebSiteCategory(string szKey) 60 { 61 if (!m_mapWebSites[szKey]) 62 m_mapWebSites[szKey] = new CConcreteWebSite(szKey); 63 64 return m_mapWebSites[szKey]; 65 } 66 67 // 获得网站分类总数 68 int GetWebSiteCount() { return m_mapWebSites.size(); } 69 }; 70 71 72 int main() 73 { 74 CWebSiteFactory WebSiteFactory; 75 76 CWebSite* fx = WebSiteFactory.GetWebSiteCategory("产品展示"); 77 fx->Use(&CUser("GHL")); 78 79 CWebSite* fy = WebSiteFactory.GetWebSiteCategory("产品展示"); 80 fy->Use(&CUser("GGG")); 81 82 CWebSite* fz = WebSiteFactory.GetWebSiteCategory("产品展示"); 83 fz->Use(&CUser("HHH")); 84 85 CWebSite* fl = WebSiteFactory.GetWebSiteCategory("博客"); 86 fl->Use(&CUser("LLL")); 87 88 CWebSite* fm = WebSiteFactory.GetWebSiteCategory("博客"); 89 fm->Use(&CUser("ggg")); 90 91 CWebSite* fn = WebSiteFactory.GetWebSiteCategory("博客"); 92 fn->Use(&CUser("hhh")); 93 94 cout << "网站分类总数为: " << WebSiteFactory.GetWebSiteCount() << endl; 95 96 system("pause"); 97 return 0; 98 }