当前位置:   article > 正文

C++设计模式-享元模式_c++享元模式

c++享元模式

目录

 

 

基本概念

代码与实例


 

基本概念

享元模式(Flyweight):运用共享技术有效的支持大量细粒度的对象。

UML图如下(此图来源于大话设计模式

享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能受大幅度的减少需要实例化的数量。如果能把哪些参数移动到类实例外面,在方法调用是将他们传递进来,就可以通过共享大幅度的减少单个实例的数量。

如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就可以考虑使用;对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。

 

 

代码与实例

程序运行截图如下:

源码如下:

Head.h

  1. #ifndef HEAD_H
  2. #define HEAD_H
  3. #include <iostream>
  4. #include <cstring>
  5. #include <map>
  6. #include <algorithm>
  7. using namespace std;
  8. class Flyweight;
  9. typedef pair<char, Flyweight*> in_pair;
  10. typedef pair<map<char, Flyweight*>::iterator, bool> in_pair_bool;
  11. //Flyweight类,它是所有具体享元类的超类或接口,通过这个接口,Flyweight可以接收并作用于外部状态
  12. class Flyweight{
  13. public:
  14. virtual void operation(const int &extrinsicstate);
  15. virtual ~Flyweight();
  16. };
  17. //ConcreteFlyweight是继承Flyweight超类或实现Flyweight接口,并为内部状态增加存储空间
  18. class ConcreteFlyweight: public Flyweight{
  19. public:
  20. ConcreteFlyweight();
  21. void operation(const int &extrinsicstate);
  22. ~ConcreteFlyweight();
  23. };
  24. //UnsharedConcreteFlyweight是指那些不需要共享的Flyweight子类。因为Flyweight接口共享成为可能,但它并不强制共享
  25. class UnsharedConcreteFlyweight: public Flyweight{
  26. public:
  27. void operation(const int &extrinsicstate);
  28. ~UnsharedConcreteFlyweight();
  29. };
  30. //FlyweightFactory是一个享元工厂,用来创建并且管理Flyweight对象。用来确保合理的共享Flyweight
  31. //当用户请求一个Flyweight时,FlyweightFacotry对象提供一个已创建的实例或者创建一个(如果不存在的话)
  32. class FlyweightFacory{
  33. public:
  34. FlyweightFacory();
  35. ~FlyweightFacory();
  36. Flyweight* getFlyweight(char c);
  37. protected:
  38. void insertOk(in_pair_bool pr);
  39. friend void deleteMapNode(in_pair pr);
  40. private:
  41. map<char, Flyweight*> m_flyweights;
  42. };
  43. #endif //HEAD_H

Head.cpp

  1. #include "Head.h"
  2. void Flyweight::operation(const int &extrinsicstate)
  3. {
  4. }
  5. Flyweight::~Flyweight()
  6. {
  7. }
  8. ConcreteFlyweight::ConcreteFlyweight()
  9. {
  10. }
  11. void ConcreteFlyweight::operation(const int &extrinsicstate)
  12. {
  13. cout << "具体Flyweight:" << extrinsicstate << endl;
  14. }
  15. ConcreteFlyweight::~ConcreteFlyweight()
  16. {
  17. cout << "ConcreteFlyweight::~ConcreteFlyweight()" << endl;
  18. }
  19. void UnsharedConcreteFlyweight::operation(const int &extrinsicstate)
  20. {
  21. cout << "不共享的具体类Flyweight:" << extrinsicstate << endl;
  22. }
  23. UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()
  24. {
  25. cout << "UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()" << endl;
  26. }
  27. FlyweightFacory::FlyweightFacory()
  28. {
  29. insertOk(m_flyweights.insert(in_pair('x', new ConcreteFlyweight)));
  30. insertOk(m_flyweights.insert(in_pair('y', new ConcreteFlyweight)));
  31. insertOk(m_flyweights.insert(in_pair('z', new ConcreteFlyweight)));
  32. }
  33. FlyweightFacory::~FlyweightFacory()
  34. {
  35. cout << "FlyweightFacory::~FlyweightFacory()" << endl;
  36. for_each(m_flyweights.begin(), m_flyweights.end(), deleteMapNode);
  37. m_flyweights.clear();
  38. }
  39. Flyweight* FlyweightFacory::getFlyweight(char c)
  40. {
  41. map<char, Flyweight*>::iterator i = m_flyweights.find(c);
  42. return i->second;
  43. }
  44. void FlyweightFacory::insertOk(in_pair_bool pr)
  45. {
  46. if(pr.second){
  47. cout << "insert ok!" << endl;
  48. }
  49. else{
  50. cout << "insert failed!" << endl;
  51. }
  52. }
  53. void deleteMapNode(in_pair pr)
  54. {
  55. delete pr.second;
  56. }

main.cpp

  1. #include "Head.h"
  2. int main(int *argc, int *argv[]){
  3. int extrinsicstate = 22;
  4. FlyweightFacory *f = new FlyweightFacory;
  5. Flyweight *fx = f->getFlyweight('x');
  6. fx->operation(--extrinsicstate);
  7. Flyweight *fy = f->getFlyweight('y');
  8. fy->operation(--extrinsicstate);
  9. Flyweight *fz = f->getFlyweight('z');
  10. fz->operation(--extrinsicstate);
  11. Flyweight *uf = new UnsharedConcreteFlyweight;
  12. uf->operation(--extrinsicstate);
  13. delete f;
  14. getchar();
  15. return 0;
  16. }

实际上FlyweightFactory不用在构造函数里面,可以根据需要在进行添加等!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/600015
推荐阅读
相关标签
  

闽ICP备14008679号