1、享元模式运用共享技术有效地支持大量细粒度的对象。
UML图如下:
2、思考:Flyweight根据客户需求返回已经生成好的对象,但一定要事先生成对象实例吗?
答:实际上是不一定需要的,完全可以初始化的时候什么也不做,到需要的时候,再去判断对象是否为NULL来决定是否实例化。
3、思考:为什么要有UnsharedConcreteFlyweight的存在呢?
答:因为尽管我们大部分时间都需要共享对象来降低内存的消耗,但个别时候也有可能不需要共享的,那么此时的UnsharedConcreteFlyweight子类就有存在的必要了,它可以解决那些不需要共享对象的问题。
4、内部状态和外部状态
在享元对象内部并且不会随环境改变而改变的共享部分,可以称为是享元对象的内部状态;而随着环境变化而变化的不可共享的状态就是说外部状态了。
事实上,享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能够大幅度地减小需要实例化的类的数量。如果能把那些参数移到类实例的外面,在方法调用时将它们传递过来,就可以通过共享大幅度地减少单个实例的数目。
也就是说,享元模式Flyweight执行时所需的状态是有内部的也有可能有外部状态的,内部状态存储在ConcreteFlyweight中,而对象的外部状态考虑有客户端对象存储或计算,当调用Flyweight对象的操作时,将该状态传递给它。
5、享元模式的应用:什么时候使用享元模式?
答:如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑使用;还有就是对象的大多数状态可以是外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。
- #include <iostream>
- #include <cstring>
- using namespace std;
-
- /* Flyweight类,它是所有具体享元类的
- 超类或接口,通过这个接口,Flyweight
- 可以接受并作用于外部状态 */
- class Flyweight
- {
- public:
- char name[10];
- virtual void Operation(int) = 0;
- };
- /* ConcreteFlyweight是继承了Flyweight超类或实现
- Flyweight接口,并为内部状态增加存储空间 */
- class ConcreteFlyweight : public Flyweight
- {
- public:
- void Operation(int extrinsicstate)
- {
- cout <<"具体Flyweight_" << name <<":" << extrinsicstate << endl;
- }
- };
- /* UnsharedConcreteFlyweight是指那些不需要共享的
- Flyweight子类。因为Flyweight接口共享成为可能
- 但它不强制共享 */
- class UnsharedConcreteFlyweight : public Flyweight
- {
- public:
- UnsharedConcreteFlyweight(char * str)
- {
- strcpy(name, str);
- }
- void Operation(int extrinsicstate)
- {
- cout << "不共享的具体Flyweight_" << name <<":" << extrinsicstate << endl;
- }
- };
- /* node 节点 */
- struct node
- {
- node * next[26];
- Flyweight * pFlyweight;
- };
- /* 自己写的伪map */
- class MyMap
- {
- private:
- node * root;
- public:
- MyMap();
- node * getNode();
- void insert(char * str, Flyweight * pFlyweight);
- Flyweight * query(char * str);
- };
- node * MyMap::getNode() {
- node * pNode = (node *)malloc(sizeof(node));
- pNode->pFlyweight = NULL;
- memset(pNode->next, 0, sizeof(pNode->next));
- return pNode;
- }
- MyMap::MyMap()
- {
- root = getNode();
- }
- void MyMap::insert(char * str, Flyweight * pFlyweight)
- {
- node * pCur = root;
- int len = strlen(str);
- for (int i = 0; i < len; ++i)
- {
- int index = str[i] - 'a';
- if (pCur->next[index] == NULL)
- {
- pCur->next[index] = getNode();
- }
- pCur = pCur->next[index];
- }
- pCur->pFlyweight = pFlyweight;
- strcpy(pCur->pFlyweight->name, str);
- }
- Flyweight * MyMap::query(char * str)
- {
- node * pCur = root;
- int len = strlen(str);
- for (int i = 0; i < len; ++i)
- {
- int index = str[i] - 'a';
- if (pCur->next[index] == NULL)
- {
- return NULL;
- }
- pCur = pCur->next[index];
- }
- return pCur->pFlyweight;
- }
- /*
- FlyweightFactory是一个享元工厂,用来创建并
- 管理Flyweight对象,它主要是用来确保合理地
- 共享Flyweight对象提供一个已创建的实例或者
- 创建一个(如果不存在)
- */
- class FlyweightFactory
- {
- private:
- MyMap map;
- public:
- FlyweightFactory()
- {
- map.insert("x", new ConcreteFlyweight());
- map.insert("y", new ConcreteFlyweight());
- map.insert("z", new ConcreteFlyweight());
- }
- Flyweight * getFlyweight(char * str)
- {
- return map.query(str);
- }
- };
- int main()
- {
- //代码外部状态
- int extrinsicstate = 22;
- FlyweightFactory factory;
- Flyweight * pFx = factory.getFlyweight("x");
- pFx->Operation(--extrinsicstate);
- Flyweight * pFy= factory.getFlyweight("y");
- pFy->Operation(--extrinsicstate);
- Flyweight * pFz = factory.getFlyweight("z");
- pFz->Operation(--extrinsicstate);
- Flyweight * pUnsharedF = new UnsharedConcreteFlyweight("u");
- pUnsharedF->Operation(--extrinsicstate);
- return 0;
- }