1.意图

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

2.动机

Flyweight模式描述了如何共享对象,使得可以细粒度地使用它们,而无需高昂的代价。flyweight是一个共享对象,它可以同时在多个场景(context)中使用,并且在每个场景中flyweight都可以作为一个独立的对象---这一点与非共享对象的实例没有区别。

3.适用性

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

4.结构图

5.代码实例

//Flyweight.h

#include <string>

class FlweightGraphicBase
{
public:
virtual void PrintColor()=;
protected:
std::string m_Color;
}; class FlweightWhiteGraphic : public FlweightGraphicBase
{
public:
FlweightWhiteGraphic();
void PrintColor();
}; class FlweightBlackGraphic : public FlweightGraphicBase
{
public:
FlweightBlackGraphic();
void PrintColor();
};
//FlyweigtFactory.h

#include <string>
#include <memory>
#include <map> class FlweightGraphicBase; class FlyweightFactory
{
public:
std::shared_ptr<FlweightGraphicBase> GetFlyweight(std::string sColor);
void SetFlyweight(std::string sColor,
std::shared_ptr<FlweightGraphicBase> pFlyweight); private:
std::map <std::string,std::shared_ptr<FlweightGraphicBase>> m_mapFlyweight;
};
//Flyweight.cpp

#include "Flyweight.h"
#include <iostream> FlweightWhiteGraphic::FlweightWhiteGraphic()
{
m_Color = "White";
} void FlweightWhiteGraphic :: PrintColor()
{
std::cout<<"Color is :" << m_Color << std::endl;
} FlweightBlackGraphic::FlweightBlackGraphic()
{
m_Color = "Black";
} void FlweightBlackGraphic :: PrintColor()
{
std::cout<<"Color is :" << m_Color << std::endl;
}
//FlyweightFactory.cpp

#include "FlyweightFactory.h"

std::shared_ptr<FlweightGraphicBase> FlyweightFactory::GetFlyweight(std::string sColor)
{
auto iter = m_mapFlyweight.find(sColor);
if(iter == m_mapFlyweight.end())
{
return nullptr;
} return iter->second;
} void FlyweightFactory::SetFlyweight(std::string sColor,
std::shared_ptr<FlweightGraphicBase> pFlyweight)
{
m_mapFlyweight[sColor] = pFlyweight;
}
//Client.cpp

#include "Flyweight.h"
#include "FlyweightFactory.h"
#include <iostream> void Insert(std::string sColor,
int index,std::shared_ptr<FlweightGraphicBase> pFlweightGraphic)
{
std::cout<<"Posion " << index << " Insert: " << sColor << std::endl ;
} int main()
{
std::shared_ptr<FlyweightFactory> pFlyweightFactory(new FlyweightFactory);
std::shared_ptr<FlweightWhiteGraphic> pFlweightWhiteGraphic(new FlweightWhiteGraphic);
std::shared_ptr<FlweightBlackGraphic> pFlweightBlackGraphic(new FlweightBlackGraphic); pFlyweightFactory->SetFlyweight("White",pFlweightWhiteGraphic);
pFlyweightFactory->SetFlyweight("Black",pFlweightBlackGraphic); auto pFlyweight = pFlyweightFactory->GetFlyweight("White");
Insert("White",,pFlyweight); pFlyweight = pFlyweightFactory->GetFlyweight("Black");
Insert("Black",,pFlyweight); pFlyweight = pFlyweightFactory->GetFlyweight("White");
Insert("White",,pFlyweight); pFlyweight = pFlyweightFactory->GetFlyweight("Black");
Insert("Black",,pFlyweight); while(); }

6.测试结果

实例代码中 所有插入的Graphic 共享黑白两种颜色,位置信息存储在外部。

7.效果

  • 存储节约,和以下因素有关:
  • 1.因为共享,实例总数减少的数目。
  • 2.对象内部状态的平均数目。
  • 3.外部状态是计算的还是存储的。

最新文章

  1. 脚本改yum源
  2. Titanium中调用ios组件时语言不是本地化的解决方法
  3. Java8简单的本地缓存实现
  4. 理解NDCG
  5. mac media server
  6. 错误 1 未能找到元数据文件“C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/wwwroot/7cb4fcd
  7. 【转】Android adb shell操作时出现“ XXX ... Read-only file system”解决办法--不错
  8. Grunt的配置和使用(一)
  9. Apache HttpServer Installing the apache2.2 service &lt;OS 5&gt;拒绝访问. :Failed to open the WinNT service manager
  10. html标签的嵌套规则分析
  11. Raphael的transform用法
  12. Oil Deposits
  13. windows下使用caffe测试mnist数据集
  14. consul配置和使用
  15. eclipse maven .jar中没有主清单属性
  16. prometheus热重启
  17. 【ZJOI 2019】麻将(dp of dp)
  18. 【Ruby】【YAML】
  19. web前端基础知识!
  20. C++指针总结

热门文章

  1. 第一篇英文短文《It All Starts With A Dream》
  2. NOIP复赛
  3. xfce4 启用回收站
  4. 软件测试之loadrunner学习笔记-02集合点
  5. 软件测试之loadrunner学习笔记-01事务
  6. matlab函数_连通区域
  7. TabLayout 简单使用。
  8. Python交互式编程导论----事件驱动编程
  9. PHP中类自动加载的方式
  10. oracle驱动问题