Flyweight(享元)--对象结构型模式
2024-08-27 15:32:37
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.外部状态是计算的还是存储的。
最新文章
- 脚本改yum源
- Titanium中调用ios组件时语言不是本地化的解决方法
- Java8简单的本地缓存实现
- 理解NDCG
- mac media server
- 错误 1 未能找到元数据文件“C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/wwwroot/7cb4fcd
- 【转】Android adb shell操作时出现“ XXX ... Read-only file system”解决办法--不错
- Grunt的配置和使用(一)
- Apache HttpServer Installing the apache2.2 service <;OS 5>;拒绝访问. :Failed to open the WinNT service manager
- html标签的嵌套规则分析
- Raphael的transform用法
- Oil Deposits
- windows下使用caffe测试mnist数据集
- consul配置和使用
- eclipse maven .jar中没有主清单属性
- prometheus热重启
- 【ZJOI 2019】麻将(dp of dp)
- 【Ruby】【YAML】
- web前端基础知识!
- C++指针总结