FlyWeight 享元模式

本文介绍了一种使用C++实现享元模式的方法,通过抽象基类和具体子类演示了如何利用享元工厂来管理和复用对象,从而减少内存消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
class FlyWeight
{
protected:
  std::string m_name;
public:
  FlyWeight(std::string str):m_name(str){}
  virtual ~FlyWeight(){cout<<"FlyWeight 基类析构"<<endl;}
  std::string GetName()const{return m_name;}
};
class FlyWeightConcreteA:public FlyWeight
{
public:
  virtual ~FlyWeightConcreteA(){cout<<"FlyWeightConcreteA 子类析构"<<endl;}
  FlyWeightConcreteA(std::string str):FlyWeight(str + "A"){}
};
class FlyWeightConcreteB:public FlyWeight
{
public:
  virtual ~FlyWeightConcreteB(){cout<<"FlyWeightConcreteB 子类析构"<<endl;}
  FlyWeightConcreteB(std::string str):FlyWeight(str + "B"){}
};
class FlyWeightFactory
{
protected:
  vector<FlyWeight*> m_vector;
public:
  virtual FlyWeight* GetFlyWeight(std::string str) = 0;
  virtual ~FlyWeightFactory()
  {
    cout<<"FlyWeightFactory 基类析构"<<endl;
    vector<FlyWeight*>::iterator it = m_vector.begin(),itend = m_vector.end();
    for(;it != itend; it++)
    {
      delete *it;
    }
    m_vector.clear();
  }
};
class FlyWeightFactoryA:public FlyWeightFactory
{
public:
  virtual ~FlyWeightFactoryA(){cout<<"FlyWeightFactoryA 子类析构"<<endl;}
  virtual FlyWeight* GetFlyWeight(std::string str)
  {
    vector<FlyWeight*>::iterator it = m_vector.begin(),itend = m_vector.end();
    for(;it != itend; it++)
    {
      if((*it)->GetName() == (str + "A"))return *it;
    }
    FlyWeight* pFly = new FlyWeightConcreteA(str);
    m_vector.push_back(pFly);
    return pFly;
  }
};
class FlyWeightFactoryB:public FlyWeightFactory
{
public:
  virtual ~FlyWeightFactoryB(){cout<<"FlyWeightFactoryB 子类析构"<<endl;}
  virtual FlyWeight* GetFlyWeight(std::string str)
  {
    vector<FlyWeight*>::iterator it = m_vector.begin(),itend = m_vector.end();
    for(;it != itend; it++)
    {
      if((*it)->GetName() == (str + "B"))return *it;
    }
    FlyWeight* pFly = new FlyWeightConcreteB(str);
    m_vector.push_back(pFly);
    return pFly;
  }
};
void Do(FlyWeightFactory* pFac,int x,int y)
{
  FlyWeight* pFly = pFac->GetFlyWeight("hello");
  cout<<"数出字符串:"<<pFly->GetName()<<"在x="<<x<<",y="<<y<<endl;
  pFly = pFac->GetFlyWeight("world");
  cout<<"数出字符串:"<<pFly->GetName()<<"在x="<<x<<",y="<<y<<endl;
  pFly = pFac->GetFlyWeight("hello");
  cout<<"数出字符串:"<<pFly->GetName()<<"在x="<<x<<",y="<<y<<endl;
}
int main(int argc, char *argv[])
{
    FlyWeightFactory* oneFac = new FlyWeightFactoryA;
    cout<<"----------------"<<endl;
    Do(oneFac,10,10);
    cout<<"----------------"<<endl;
    delete oneFac;
    oneFac = new FlyWeightFactoryB;
    cout<<"----------------"<<endl;
    Do(oneFac,100,100);
    cout<<"----------------"<<endl;
    delete oneFac;
    system("PAUSE");
    return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值