设计模式学习笔记(C++实现)(十二)--享元模式

1.享元模式简介

  • 英文名称
    Flyweight
  • 主要目的
    利用享元模式,可以有效地支持大量细粒度的对象。
  • 使用场景
    1.一个应用程序使用了大量的对象;
    2.由于完全使用大量的对象,造成很大的存储开销;
    3.对象的大多数状态都可以变为外部状态;
    4.如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象;
    5.应用程序不依赖于对象标识。

2.享元模式代码示例

  • 测试平台
    1.开发语言:C++
    2.开发工具:VS2015
    3.操作系统:Win7 X64
  • 代码说明
    1.Customer–顾客类;
    2.Computer–抽象电脑类,提供基本接口;
    3.ConcreateComputer–具体电脑类;
    4.ComputerFactory-电脑工程类,实现享元模式。

    注意:
    1.本例用电脑DIY店来说明享元模式;
    2.对于商家来说,有多种多样的客户,但是电脑配置数量是有限的。如果有新的配置,就自动加入到配置库中,如果是已有配置,就直接从配置库中选取。

  • 具体代码

#include <iostream>
#include <map>
using namespace std;

//顾客类
class Customer
{
public:
    Customer(string name):
        m_Name(name) {}

    string GetName()
    {
        return this->m_Name;
    }
private:
    string m_Name;
};

//电脑抽象类
class Computer
{
public:
    virtual void Purchase(Customer customer) = 0;                 //抽象类
};

//具体电脑类
class ConcreateComputer :public Computer
{
public:
    ConcreateComputer(string style):
        m_Style(style)
    {}

    void Purchase(Customer customer)
    {
        cout << "电脑款式:" << this->m_Style.c_str()  << ",购买者:" << customer.GetName().c_str() << endl;
    }
private:
    string m_Style;
};

//电脑工厂类
class ComputerFactory
{   
public:
    //获取电脑款式
    Computer* GetComputerStyle(string style)
    {
        map<string, Computer*>::iterator it;
        it = this->m_FlyWeights.find(style);
        if (it==this->m_FlyWeights.end())
        {
            this->m_FlyWeights.insert(make_pair(style, new ConcreateComputer(style)));
        }

        return m_FlyWeights[style];
    }

    //析构函数,负责释放申请的内存
    ComputerFactory()
    {
        map<string, Computer*>::iterator iter;
        for (iter = this->m_FlyWeights.begin(); iter != this->m_FlyWeights.end(); ++iter)
        {
            if (iter->second != NULL)
            {
                delete iter->second;
            }
        }

        this->m_FlyWeights.clear();
    }

    //获得电脑款式总数
    int GetComputerSytleCount()
    {
        return m_FlyWeights.size();
    }
private:
    map<string, Computer*> m_FlyWeights;
};


int main()
{
    ComputerFactory* pComputerFactory = new ComputerFactory();
    if (pComputerFactory !=NULL)
    {   
        //顾客小张,购买游戏发烧友款电脑
        Customer xiaoWang("小王");
        Computer* pComputerXiaoWang = pComputerFactory->GetComputerStyle("游戏发烧款");
        pComputerXiaoWang->Purchase(xiaoWang);

        //顾客小李,购买家用影音款电脑
        Customer xiaoLi("小李");
        Computer* pComputerXiaoLi = pComputerFactory->GetComputerStyle("家用影音款");
        pComputerXiaoLi->Purchase(xiaoLi);

        //顾客小赵,购买基础办公款电脑
        Customer xiaoZhao("小赵");
        Computer* pComputerXiaoZhao = pComputerFactory->GetComputerStyle("基础办公款");
        pComputerXiaoZhao->Purchase(xiaoZhao);

        //顾客小张,购买基础办公款电脑
        Customer xiaoZhang("小张");
        Computer* pComputerXiaoZhang = pComputerFactory->GetComputerStyle("基础办公款");
        pComputerXiaoZhang->Purchase(xiaoZhang);

        cout << "本店所售电脑款式:" << pComputerFactory->GetComputerSytleCount() << endl;

        delete pComputerFactory;
        pComputerFactory = NULL;
    }

    getchar();
    return 0;
}
  • 输出结果
    这里写图片描述

栏目导航
上一篇:设计模式学习笔记(C++实现)(十一)–外观模式
下一篇:设计模式学习笔记(C++实现)(十三)–代理模式

参考文献:
1.《设计模式:可复用面向对象软件的基础》
2.《大话设计模式》

猜你喜欢

转载自blog.csdn.net/u014337397/article/details/80446138