Flyweight Pattern

1.Flyweight 模式以共享的方式高效的支持大量的细粒度对象,对象分为内部状态、外部状态。将可以被共享的状态作为内部状态存储在对象中,而外部状态在适当的时候作为参数传递给对象。

当以下所有的条件都满足时,可以考虑使用享元模式:

  • 一个系统有大量的对象。
  • 这些对象耗费大量的内存。
  • 这些对象的状态中的大部分都可以外部化。
  • 这些对象可以按照内蕴状态分成很多的组,当把外蕴对象从对象中剔除时,每一个组都可以仅用一个对象代替。
  • 软件系统不依赖于这些对象的身份,换言之,这些对象可以是不可分辨的。

2.Flyweight模式结构图(不想被共享的对象UnshaerConcreteFlyweight,暂不讨论)

Flyweight Pattern_第1张图片

3.实现

 1 #ifndef _FLYWEIGHT_H_ 
 2 #define _FLYWEIGHT_H_
 3 
 4 #include <string> 
 5 using namespace std;
 6 
 7 class Flyweight 
 8 { 
 9 public:
10     virtual ~Flyweight();
11     virtual void Operation(const string& extrinsicState);
12     string GetIntrinsicState();
13 protected: 
14     Flyweight(string intrinsicState);
15 private: 
16     string _intrinsicState;
17 };
18 
19 class ConcreteFlyweight:public Flyweight 
20 { 
21 public: 
22     ConcreteFlyweight(string intrinsicState);
23     ~ConcreteFlyweight();
24     void Operation(const string& extrinsicState);
25 protected:
26 private: 
27 }; 
28 
29 #endif
Flyweight.h
 1 #include "Flyweight.h" 
 2 #include <iostream> 
 3 using namespace std;
 4 
 5 Flyweight::Flyweight(string intrinsicState) 
 6 { 
 7     this->_intrinsicState = intrinsicState;
 8 }
 9 Flyweight::~Flyweight() 
10 {
11 
12 }
13 void Flyweight::Operation(const string& extrinsicState) 
14 {
15 
16 }
17 string Flyweight::GetIntrinsicState() 
18 { 
19     return this->_intrinsicState; 
20 }
21 ConcreteFlyweight::ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState) 
22 { 
23     cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;
24 }
25 ConcreteFlyweight::~ConcreteFlyweight() 
26 {
27 
28 }
29 void ConcreteFlyweight::Operation(const string& extrinsicState) 
30 { 
31     cout<<"ConcreteFlyweight:内蕴["<<this->GetIntrinsicState()<<"] 外蕴["<<extrinsicState<<"]"<<endl; 
32 }
Flyweight.cpp
 1 #ifndef _FLYWEIGHTFACTORY_H_
 2 #define _FLYWEIGHTFACTORY_H_
 3 
 4 #include "Flyweight.h" 
 5 #include <string> 
 6 #include <vector> 
 7 using namespace std;
 8 
 9 class FlyweightFactory 
10 { 
11 public: 
12     FlyweightFactory();
13     ~FlyweightFactory();
14     Flyweight* GetFlyweight(const string& key);
15 protected:
16 private: 
17     vector<Flyweight*> _fly;
18 }; 
19 
20 #endif
FlyweightFactory.h
 1 #include "FlyweightFactory.h" 
 2 #include <iostream> 
 3 #include <string> 
 4 #include <cassert> 
 5 using namespace std;
 6 using namespace std;
 7 
 8 FlyweightFactory::FlyweightFactory()
 9 {
10 
11 }
12 FlyweightFactory::~FlyweightFactory() 
13 {
14 
15 }
16 Flyweight* FlyweightFactory::GetFlyweight(const string& key)
17 { 
18     vector<Flyweight*>::iterator it = _fly.begin();
19     for (; it != _fly.end();it++) 
20     { //找到了,就一起用,^_^ 
21         if ((*it)->GetIntrinsicState() == key) 
22         { 
23             cout<<"already created by users...."<<endl;
24             return *it; 
25         } 
26     }
27     Flyweight* fn = new ConcreteFlyweight(key);
28     _fly.push_back(fn);
29     return fn; 
30 }
FlyweightFactory.cpp
 1 #include "Flyweight.h" 
 2 #include "FlyweightFactory.h" 
 3 #include <iostream> 
 4 using namespace std;
 5 
 6 int main(int argc,char* argv[]) 
 7 { 
 8     FlyweightFactory* fc = new FlyweightFactory();
 9     Flyweight* fw1 = fc->GetFlyweight("hello"); 
10     Flyweight* fw2 = fc->GetFlyweight("world!");
11     Flyweight* fw3 = fc->GetFlyweight("hello");
12     return 0; 
13 }
main.cpp

 

你可能感兴趣的:(flyweight)