享元模式(Flyweight)案例分析

Flyweight 享元设计模式是一种结构型设计模式,它主要解决的问题是:由于(同类)对象的数量太大,采用面向对象时给系统带来了难以承受的内存开销。比如有这样一个场景:一个停车场中有 1000 辆汽车,我们所定义的汽车对象占用内存 0.3M ,那么要实例化 1000 辆就是 300M 。由此可见,在这种情况下采用一般的面向对象方式出现了大量细粒度的对象会很快充斥在系统中,从而带来很高的运行是代价(这里指的代价是内存开销的代价)。

       GoF《设计模式》中说道:运用共享技术有效的支持大量细粒度的对象。

 

案例分析

      定义一个场景:有一个汽车类型,客户程序要实例化20000个,实例化后查看一下内存分配情况。

 

 

#include "stdafx.h"
#include "windows.h"
#include "iostream.h"
#include <string>
#include <list>
#include <map>
#pragma warning (disable: 4786)

/*
class Car
{
    private:
  std::string Body;
  std::string Wheel;
  std::string Engine;
  std::string Brand;
  std::string Color;  

    public:
  Car(std::string body,std::string wheel,std::string engine,std::string brand,std::string color)
  {
   Body = body;
   Wheel = wheel;
   Engine = engine;
   Brand = brand;
   Color = color;
  }

};

int main(int argc, char* argv[])
{
 unsigned long MemCount = 0;
 MEMORYSTATUS  MemInfo;  
 MemInfo.dwLength = sizeof(MEMORYSTATUS);  
    GlobalMemoryStatus(&MemInfo);

 cout << "内存分配情况如下:" << endl;
 MemCount = MemInfo.dwAvailVirtual;
 cout << "实例化前:" << MemInfo.dwAvailVirtual << endl;

 std::list<Car> m_list;
 for(int i = 0;i < 20000;i++) 
 {
  Car * car = new Car("4.2M","Wheel","NeedForSpeed","BMW","Black");
  //m_list.push_back(*car);
 }

    GlobalMemoryStatus(&MemInfo);
 cout << "实例化后:" << MemInfo.dwAvailVirtual << endl;
 cout << "实例化后:" << MemCount - MemInfo.dwAvailVirtual << endl;

 return 0;
}
*/

/*
内存分配情况如下:
实例化前:2140868608
实例化后:2125139968
实例化后:15728640
*/


class CarBody

 private:
  std::string Body; 
  std::string Wheel; 
  std::string Engine;
  std::string Color;  
 
 public:
  CarBody(){};
  CarBody(std::string body,std::string wheel,std::string engine,std::string color)
  {
   Body = body; 
   Wheel = wheel;
   Engine = engine;
   Color = color; 
  }
};

class FlyWeightCar
{
     public:
   std::string Brand;
   CarBody * carbody;

  public:
   FlyWeightCar(){};
};


class FlyWeightFactory
{

private:
 static FlyWeightCar * Car;
    static std::map<std::string,FlyWeightCar> table;

public:
 static FlyWeightCar * CreateInit(std::string body,std::string wheel,std::string engine,std::string brand,std::string color)
    {
   FlyWeightCar * car = Car;
         if(brand.compare("BMW") == 0)
         {
              //car = &(FlyWeightCar)table[brand];
     car = Car;
         }
         else
         {
              car->Brand = brand;
              car->carbody = new CarBody(body,wheel,engine,color);
     std::pair<std::string,FlyWeightCar> item (brand, *car);
              table.insert(item);
         }
         return car;
    }
};

FlyWeightCar * FlyWeightFactory::Car = new FlyWeightCar();

std::map<std::string,FlyWeightCar> FlyWeightFactory::table;

int main(int argc, char* argv[])
{
 unsigned long MemCount = 0;
 MEMORYSTATUS  MemInfo;  
 MemInfo.dwLength = sizeof(MEMORYSTATUS);  
    GlobalMemoryStatus(&MemInfo);

 cout << "内存分配情况如下:" << endl;
 MemCount = MemInfo.dwAvailVirtual;
 cout << "实例化前:" << MemInfo.dwAvailPhys  << endl;

 std::list<FlyWeightCar> m_list;
 for(int i = 0;i < 10000;i++) 
 {
  FlyWeightCar * car = FlyWeightFactory::CreateInit("4.2M","Wheel","NeedForSpeed","Benz","Black");
  //m_list.push_back(*car);
 }
 
 for(i = 0;i < 10000;i++) 
 {
  FlyWeightCar * car = FlyWeightFactory::CreateInit("4.2M","Wheel","NeedForSpeed","BMW","Black");
  //m_list.push_back(*car);
 }
   
    GlobalMemoryStatus(&MemInfo);
 cout << "实例化后:" << MemInfo.dwAvailPhys  << endl;

    cout << "占用内存:" << MemCount - MemInfo.dwAvailVirtual << endl;
 return 0;
}


/*
内存分配情况如下:
实例化前:328978432
实例化后:324227072
占用内存:7340032
*/

 

    从数字上不难看出内存分配的容量节省了不少,而且随着数量的增加,差距会更大,当数量教少时,普通方式的内存分配更小一些,所以,在使用时,我们一定要对所实例化对象的个数进行评估,否则的话会适得其反。

      

Flyweight模式的几个要点:

       1、面向对象很好的解决了抽象性的问题,但是作为一个运行在机器中的程序实体,我们需要考虑对象的代价问题。Flyweight设计模式主要解决面向对象的代价问题,一般不触及面向对象的抽象性问题。

2Flyweight采用对象共享的做法来降低系统中对象的个数,从而降低细粒度对象给系统带来的内存压力。在具体实现方面,要注意对象的状态处理。

3、对象的数量太大从而导致对象内存开销加大(这个数量要经过评估,而不能凭空臆断)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值