赞
踩
目录
享元模式(Flyweight):运用共享技术有效的支持大量细粒度的对象。
UML图如下(此图来源于大话设计模式)
享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能受大幅度的减少需要实例化的数量。如果能把哪些参数移动到类实例外面,在方法调用是将他们传递进来,就可以通过共享大幅度的减少单个实例的数量。
如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就可以考虑使用;对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。
程序运行截图如下:
源码如下:
Head.h
- #ifndef HEAD_H
- #define HEAD_H
-
- #include <iostream>
- #include <cstring>
- #include <map>
- #include <algorithm>
- using namespace std;
-
- class Flyweight;
-
- typedef pair<char, Flyweight*> in_pair;
- typedef pair<map<char, Flyweight*>::iterator, bool> in_pair_bool;
-
- //Flyweight类,它是所有具体享元类的超类或接口,通过这个接口,Flyweight可以接收并作用于外部状态
- class Flyweight{
-
- public:
- virtual void operation(const int &extrinsicstate);
- virtual ~Flyweight();
- };
-
- //ConcreteFlyweight是继承Flyweight超类或实现Flyweight接口,并为内部状态增加存储空间
- class ConcreteFlyweight: public Flyweight{
-
- public:
- ConcreteFlyweight();
- void operation(const int &extrinsicstate);
- ~ConcreteFlyweight();
- };
-
- //UnsharedConcreteFlyweight是指那些不需要共享的Flyweight子类。因为Flyweight接口共享成为可能,但它并不强制共享
- class UnsharedConcreteFlyweight: public Flyweight{
-
- public:
- void operation(const int &extrinsicstate);
- ~UnsharedConcreteFlyweight();
- };
-
- //FlyweightFactory是一个享元工厂,用来创建并且管理Flyweight对象。用来确保合理的共享Flyweight
- //当用户请求一个Flyweight时,FlyweightFacotry对象提供一个已创建的实例或者创建一个(如果不存在的话)
- class FlyweightFacory{
-
- public:
- FlyweightFacory();
- ~FlyweightFacory();
- Flyweight* getFlyweight(char c);
-
- protected:
- void insertOk(in_pair_bool pr);
- friend void deleteMapNode(in_pair pr);
-
- private:
- map<char, Flyweight*> m_flyweights;
- };
-
-
- #endif //HEAD_H
Head.cpp
- #include "Head.h"
-
- void Flyweight::operation(const int &extrinsicstate)
- {
-
- }
-
- Flyweight::~Flyweight()
- {
-
- }
-
- ConcreteFlyweight::ConcreteFlyweight()
- {
-
- }
-
- void ConcreteFlyweight::operation(const int &extrinsicstate)
- {
- cout << "具体Flyweight:" << extrinsicstate << endl;
- }
-
- ConcreteFlyweight::~ConcreteFlyweight()
- {
- cout << "ConcreteFlyweight::~ConcreteFlyweight()" << endl;
- }
-
- void UnsharedConcreteFlyweight::operation(const int &extrinsicstate)
- {
- cout << "不共享的具体类Flyweight:" << extrinsicstate << endl;
- }
-
- UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()
- {
- cout << "UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()" << endl;
- }
-
- FlyweightFacory::FlyweightFacory()
- {
- insertOk(m_flyweights.insert(in_pair('x', new ConcreteFlyweight)));
- insertOk(m_flyweights.insert(in_pair('y', new ConcreteFlyweight)));
- insertOk(m_flyweights.insert(in_pair('z', new ConcreteFlyweight)));
- }
-
- FlyweightFacory::~FlyweightFacory()
- {
- cout << "FlyweightFacory::~FlyweightFacory()" << endl;
- for_each(m_flyweights.begin(), m_flyweights.end(), deleteMapNode);
- m_flyweights.clear();
- }
-
- Flyweight* FlyweightFacory::getFlyweight(char c)
- {
- map<char, Flyweight*>::iterator i = m_flyweights.find(c);
- return i->second;
- }
-
- void FlyweightFacory::insertOk(in_pair_bool pr)
- {
- if(pr.second){
-
- cout << "insert ok!" << endl;
- }
- else{
-
- cout << "insert failed!" << endl;
- }
- }
-
- void deleteMapNode(in_pair pr)
- {
- delete pr.second;
- }
main.cpp
- #include "Head.h"
-
- int main(int *argc, int *argv[]){
-
- int extrinsicstate = 22;
-
- FlyweightFacory *f = new FlyweightFacory;
- Flyweight *fx = f->getFlyweight('x');
- fx->operation(--extrinsicstate);
-
- Flyweight *fy = f->getFlyweight('y');
- fy->operation(--extrinsicstate);
-
- Flyweight *fz = f->getFlyweight('z');
- fz->operation(--extrinsicstate);
-
- Flyweight *uf = new UnsharedConcreteFlyweight;
- uf->operation(--extrinsicstate);
-
- delete f;
-
- getchar();
- return 0;
- }
实际上FlyweightFactory不用在构造函数里面,可以根据需要在进行添加等!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。