• C++设计模式-享元(Flyweight)


    目录

    C++设计模式-享元(Flyweight)

    一、意图

    二、适用性

    三、结构

    四、参与者

    五、代码


    C++设计模式-享元(Flyweight)

    一、意图

    运用共享技术有效地支持大量细粒度的对象。

    二、适用性

    • 一个应用程序使用了大量的对象。
    • 完全由于使用大量的对象,造成很大的存储开销。
    • 对象的大多数状态都可变为外部状态。
    • 如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。
    • 应用程序不依赖于对象标识。由于Flyweight对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。

    三、结构

     

    四、参与者

    • Flyweight

           描述一个接口,通过这个接口flyweight可以接受并作用于外部状态。

    • ConcreteFlyweight

            实现Flyweight接口,并为内部状态(如果有的话)增加存储空间。ConcreteFlyweight对象必须是可共享的。它所存储的状态必须是内部的;即,它必须独立于ConcreteFlyweight对象的场景。

    • UnsharedConcreteFlyweight

            并非所有的Flyweight子类都需要被共享。Flyweight接口使共享成为可能,但它并不强制共享。在Flyweight对象结构的某些层次,UnsharedConcreteFlyweight对象通常将ConcreteFlyweight对象作为子节点。

    • FlyweightFactory

            创建并管理flyweight对象。

            确保合理地共享flyweight。当用户请求一个flyweight时,FlyweightFactory对象提供一个已创建的实例或者创建一个(如果不存在的话)。

    • Client

            维持一个对flyweight的引用。

            计算或存储一个(多个)flyweight的外部状态。

    五、代码

    1. #include
    2. #include
    3. using namespace std;
    4. class Flyweight {
    5. public:
    6. Flyweight(string tempIntrinsicState) {
    7. this->intrinsicState = tempIntrinsicState;
    8. }
    9. string GetIntrinsicState() {
    10. return intrinsicState;
    11. }
    12. virtual void Operation(const string& tempExtrinsicState) = 0;
    13. private:
    14. string intrinsicState;
    15. };
    16. class ConcreteFlyweight :public Flyweight {
    17. public:
    18. ConcreteFlyweight(string tempIntrinsicState) :Flyweight(tempIntrinsicState) {}
    19. void Operation(const string& tempExtrinsicState) {
    20. cout << this->GetIntrinsicState() << endl;
    21. cout << tempExtrinsicState << endl;
    22. }
    23. };
    24. class UnsharedConcreteFlyweight : public Flyweight {
    25. public:
    26. UnsharedConcreteFlyweight(string tempIntrinsicState) :Flyweight(tempIntrinsicState) {}
    27. void UnsharedConcreteOperation(const string& tempExtrinsicState) {
    28. cout << tempExtrinsicState << endl;
    29. }
    30. };
    31. class FlyweightFactory {
    32. public:
    33. Flyweight* GetFlyweight(string state) {
    34. vector::iterator it = this->flyweightVector.begin();
    35. for (; it != this->flyweightVector.end(); it++) {
    36. if ((*it)->GetIntrinsicState() == state) {
    37. return *it;
    38. }
    39. }
    40. Flyweight* flyweight = new ConcreteFlyweight(state);
    41. this->flyweightVector.push_back(flyweight);
    42. return flyweight;
    43. }
    44. int GetFlyweightCount() {
    45. return this->flyweightVector.size();
    46. }
    47. private:
    48. vector flyweightVector;
    49. };
    50. int main() {
    51. string extrinsicState = "ExtrinsicState";
    52. FlyweightFactory* flyweightFactory = new FlyweightFactory();
    53. Flyweight* flyweight1 = flyweightFactory->GetFlyweight("AAA");
    54. Flyweight* flyweight2 = flyweightFactory->GetFlyweight("AAA");
    55. Flyweight* flyweight3 = flyweightFactory->GetFlyweight("BBB");
    56. flyweight1->Operation(extrinsicState);
    57. cout << flyweightFactory->GetFlyweightCount() << endl;
    58. cout << (flyweight1 == flyweight2) << endl;
    59. return 0;
    60. }

  • 相关阅读:
    MDM数据分析功能说明
    线上展厅干货 广州商迪
    如何用idm下载迅雷文件 idm怎么安装到浏览器 idm怎么设置中文
    【Tomcat】Tomcat 运行原理
    8.cmake常用命令
    安卓最佳实践之内存优化
    【 OpenGauss源码学习 —— 列存储(autoanalyze)(二)】
    Executor框架
    小程序map组件二——[蓝色波浪篇]引入并配置Vant Weapp(详细)+腾讯地图地点搜索插件获取实时位置实战
    【pen200-lab】10.11.1.217
  • 原文地址:https://blog.csdn.net/qq_40660998/article/details/133630700