Flyweight 模式

如果一个应用程序使用了太多的对象, 就会造成很大的存储开销。 特别是对于大量轻量级 (细粒度)的对象,比如在文档编辑器的设计过程中,我们如果为每个字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。例如一个字母“a”在文档中出现了100000 次,而实际上我们可以让这一万个字母“a”共享一个对象,当然因为在不同的位置可能字母“a”有不同的显示效果(例如字体和大小等设置不同) ,在这种情况我们可以为将对象的状态分为“外部状态”和“内部状态” ,将可以被共享(不会变化)的状态作为内部状态存储在对象中,而外部对象(例如上面提到的字体、大小等)我们可以在适当的时候将外部对象最为参数传递给对象(例如在显示的时候,将字体、大小等信息传递给对象) 。

Flyweight 模式可以解决上面的问题,其典型的结构图为:

Flyweight 模式_第1张图片

 1 ///////////////Flyweight.h////////////////////////
 2 #pragma once
 3 #include <string>
 4 using namespace std;
 5 class Flyweight
 6 {
 7 public:
 8     virtual ~Flyweight();
 9     virtual void Operation(const string& extrinsicState);
10     string GetIntrinsicState();
11 protected:
12     Flyweight(string intrinsicState);
13 private:
14     string  _intrinsicState ;
15 };
16 
17 class ConcreteFlyweight : public Flyweight
18 {
19 public:
20     ~ConcreteFlyweight();
21     ConcreteFlyweight(string intrinsicState);
22     void Operation(const string& extrinsicState);
23 protected:
24 private:
25 };
 1 ///////////////Flyweight.cpp//////////////////////////
 2 #include "Flyweight.h"
 3 #include <iostream>
 4 using namespace std;
 5 
 6 Flyweight::Flyweight(string intrinsicState)
 7 {
 8     _intrinsicState = intrinsicState ;
 9 }
10 Flyweight::~Flyweight()
11 {
12 
13 }
14 
15 void Flyweight::Operation(const string& extrinsicState)
16 {
17 
18 }
19 string Flyweight::GetIntrinsicState()
20 {
21     return this->_intrinsicState ;
22 }
23 
24 
25 ConcreteFlyweight::ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState)
26 {
27     cout<<"ConcreteFlyweight Build....."<<intrinsicState<<endl;
28 }
29 ConcreteFlyweight::~ConcreteFlyweight()
30 {
31 
32 }
33 void ConcreteFlyweight::Operation(const string& extrinsicState)
34 {
35     cout<<"ConcreteFlyweight: 内部["<<this->GetIntrinsicState()<<"] 外部["<<extrinsicState<<"]"<<endl;
36 }
 1 /////////////////FlyweightFactory.h////////////////////////////
 2 #pragma once
 3 #include "Flyweight.h"
 4 #include <vector>
 5 class FlyweightFactory
 6 {
 7 public:
 8     FlyweightFactory();
 9     ~FlyweightFactory();
10     Flyweight* GetFlyweight(const string& key);
11 
12 protected:
13 private:
14     vector<Flyweight*> _fly ;
15 };
 1 ///////////////////FlyweightFactory.cpp//////////////////////////////
 2 #include "FlyweightFactory.h"
 3 #include <iostream>
 4 using namespace std;
 5 FlyweightFactory::FlyweightFactory()
 6 {
 7 
 8 }
 9 FlyweightFactory::~FlyweightFactory()
10 {
11 
12 }
13 
14 Flyweight* FlyweightFactory::GetFlyweight(const string& key)
15 {
16     vector<Flyweight*>::iterator it = _fly.begin();
17     for (;it != _fly.end() ; it++)
18     {
19         if ((*it)->GetIntrinsicState() == key)
20         {
21             cout<<"already  created  by users...."<<endl; 
22             return *it ;
23         }
24     }
25 
26     Flyweight* fn = new ConcreteFlyweight(key);
27     _fly.push_back(fn);
28 
29     return fn ;
30 
31 }
 1 /////////////////////////main.cpp/////////////////////////////////////////////////
 2 #include "FlyweightFactory.h"
 3 #include "Flyweight.h"
 4 #include <iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     FlyweightFactory* fc = new FlyweightFactory();
 9     Flyweight* fw1 = fc->GetFlyweight("hello");
10     Flyweight* fw2 = fc->GetFlyweight("world");
11 
12     cout<<fw1->GetIntrinsicState()<<endl;
13     fw1->Operation("red");
14 
15     cout<<fw2->GetIntrinsicState()<<endl;
16     fw2->Operation("green");
17 
18     fc->GetFlyweight("hello")->Operation("black");
19     getchar();
20     return 0;
21 }

 

 

你可能感兴趣的:(flyweight)