设计模式 - FlyWeight 模式(享元模式)
作用:运用共享技术有效地支持大量细粒度的对象。
UML结构图:
解析:
Flyweight模式在大量使用一些可以被共享的对象的时候经常使用.比如,在QQ聊天的时候很多时候你懒得回复又不得不回复的时候,一般会用一些客套的话语敷衍别人,如"呵呵","好的"等等之类的,这些简单的答复其实每个人都是提前定义好的,在使用的时候才调用出来.Flyweight就是基于解决这种问题的思路而产生的,当需要一个可以在其它地方共享使用的对象的时候,先去查询是否已经存在了同样的对象,如果没有就生成之有的话就直接使用.因此,Flyweight模式和Factory模式也经常混用。
实现:
需要说明的是下面的实现仅仅实现了对可共享对象的使用,非可共享对象的使用没有列出,因为这个不是Flyweight模式的重点.这里的实现要点是采用一个list链表来保存这些可以被共享的对象,需要使用的时候就到链表中查询是不是已经存在了,如果不存在就初始化一个,然后返回这个对象的指针。
代码实现:
FlyWeight.h
1
2 #ifndef FLYWEIGHT_H
3 #define FLYWEIGHT_H
4
5 #include <string>
6 #include <list>
7
8 typedef std::string STATE;
9
10 class Flyweight
11 {
12 public:
13 virtual ~Flyweight(){}
14
15 STATE GetIntrinsicState();
16 virtual void Operation(STATE& ExtrinsicState) = 0;
17
18 protected:
19 Flyweight(const STATE& state)
20 :m_State(state)
21 {
22 }
23
24 private:
25 STATE m_State;
26 };
27
28 class FlyweightFactory
29 {
30 public:
31 FlyweightFactory(){}
32 ~FlyweightFactory();
33
34 Flyweight* GetFlyweight(const STATE& key);
35
36 private:
37 std::list<Flyweight*> m_listFlyweight;
38 };
39
40 class ConcreateFlyweight
41 : public Flyweight
42 {
43 public:
44 ConcreateFlyweight(const STATE& state)
45 : Flyweight(state)
46 {
47 }
48 virtual ~ConcreateFlyweight(){}
49
50 virtual void Operation(STATE& ExtrinsicState);
51 };
52
53 #endif
54
2 #ifndef FLYWEIGHT_H
3 #define FLYWEIGHT_H
4
5 #include <string>
6 #include <list>
7
8 typedef std::string STATE;
9
10 class Flyweight
11 {
12 public:
13 virtual ~Flyweight(){}
14
15 STATE GetIntrinsicState();
16 virtual void Operation(STATE& ExtrinsicState) = 0;
17
18 protected:
19 Flyweight(const STATE& state)
20 :m_State(state)
21 {
22 }
23
24 private:
25 STATE m_State;
26 };
27
28 class FlyweightFactory
29 {
30 public:
31 FlyweightFactory(){}
32 ~FlyweightFactory();
33
34 Flyweight* GetFlyweight(const STATE& key);
35
36 private:
37 std::list<Flyweight*> m_listFlyweight;
38 };
39
40 class ConcreateFlyweight
41 : public Flyweight
42 {
43 public:
44 ConcreateFlyweight(const STATE& state)
45 : Flyweight(state)
46 {
47 }
48 virtual ~ConcreateFlyweight(){}
49
50 virtual void Operation(STATE& ExtrinsicState);
51 };
52
53 #endif
54
FlyWeight.cpp
1 #include "FlyWeight.h"
2 #include <iostream>
3
4 inline STATE Flyweight::GetIntrinsicState()
5 {
6 return m_State;
7 }
8
9 FlyweightFactory::~FlyweightFactory()
10 {
11 std::list<Flyweight*>::iterator iter1, iter2, temp;
12
13 for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();
14 iter1 != iter2;
15 )
16 {
17 temp = iter1;
18 ++iter1;
19 delete (*temp);
20 }
21
22 m_listFlyweight.clear();
23 }
24
25 Flyweight* FlyweightFactory::GetFlyweight(const STATE& key)
26 {
27 std::list<Flyweight*>::iterator iter1, iter2;
28
29 for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();
30 iter1 != iter2;
31 ++iter1)
32 {
33 if ((*iter1)->GetIntrinsicState() == key)
34 {
35 std::cout << "The Flyweight:" << key << " already exits"<< std::endl;
36 return (*iter1);
37 }
38 }
39
40 std::cout << "Creating a new Flyweight:" << key << std::endl;
41 Flyweight* flyweight = new ConcreateFlyweight(key);
42 m_listFlyweight.push_back(flyweight);
43 }
44
45 void ConcreateFlyweight::Operation(STATE& ExtrinsicState)
46 {
47
48 }
49
2 #include <iostream>
3
4 inline STATE Flyweight::GetIntrinsicState()
5 {
6 return m_State;
7 }
8
9 FlyweightFactory::~FlyweightFactory()
10 {
11 std::list<Flyweight*>::iterator iter1, iter2, temp;
12
13 for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();
14 iter1 != iter2;
15 )
16 {
17 temp = iter1;
18 ++iter1;
19 delete (*temp);
20 }
21
22 m_listFlyweight.clear();
23 }
24
25 Flyweight* FlyweightFactory::GetFlyweight(const STATE& key)
26 {
27 std::list<Flyweight*>::iterator iter1, iter2;
28
29 for (iter1 = m_listFlyweight.begin(), iter2 = m_listFlyweight.end();
30 iter1 != iter2;
31 ++iter1)
32 {
33 if ((*iter1)->GetIntrinsicState() == key)
34 {
35 std::cout << "The Flyweight:" << key << " already exits"<< std::endl;
36 return (*iter1);
37 }
38 }
39
40 std::cout << "Creating a new Flyweight:" << key << std::endl;
41 Flyweight* flyweight = new ConcreateFlyweight(key);
42 m_listFlyweight.push_back(flyweight);
43 }
44
45 void ConcreateFlyweight::Operation(STATE& ExtrinsicState)
46 {
47
48 }
49
Main.cpp
1 #include "FlyWeight.h"
2
3 int main()
4 {
5 FlyweightFactory flyweightfactory;
6 flyweightfactory.GetFlyweight("hello");
7 flyweightfactory.GetFlyweight("world");
8 flyweightfactory.GetFlyweight("hello");
9
10 system("pause");
11 return 0;
12 }
13
2
3 int main()
4 {
5 FlyweightFactory flyweightfactory;
6 flyweightfactory.GetFlyweight("hello");
7 flyweightfactory.GetFlyweight("world");
8 flyweightfactory.GetFlyweight("hello");
9
10 system("pause");
11 return 0;
12 }
13