1.意圖 運用共用技術有效地支持大量細粒度的對象。 2.動機 Flyweight模式描述瞭如何共用對象,使得可以細粒度地使用它們,而無需高昂的代價。flyweight是一個共用對象,它可以同時在多個場景(context)中使用,並且在每個場景中flyweight都可以作為一個獨立的對象 這一點與非共 ...
1.意圖
運用共用技術有效地支持大量細粒度的對象。
2.動機
Flyweight模式描述瞭如何共用對象,使得可以細粒度地使用它們,而無需高昂的代價。flyweight是一個共用對象,它可以同時在多個場景(context)中使用,並且在每個場景中flyweight都可以作為一個獨立的對象---這一點與非共用對象的實例沒有區別。
3.適用性
- 一個應用程式使用了大量的對象。
- 完全由於使用大量的對象,造成很大的存儲開銷。
- 對象的大多數狀態都可變為外部狀態。
- 如果刪除對象的外部狀態,那麼可以用相對較少的共用對象取代很多組對象。
- 應用程式不依賴於對象標識。由於Flyweight對象可以被共用,對於概念上明顯有別的對象,標識測試將返回真值。
4.結構圖
5.代碼實例
//Flyweight.h #include <string> class FlweightGraphicBase { public: virtual void PrintColor()=0; 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",0,pFlyweight); pFlyweight = pFlyweightFactory->GetFlyweight("Black"); Insert("Black",1,pFlyweight); pFlyweight = pFlyweightFactory->GetFlyweight("White"); Insert("White",2,pFlyweight); pFlyweight = pFlyweightFactory->GetFlyweight("Black"); Insert("Black",3,pFlyweight); while(1); }
6.測試結果
實例代碼中 所有插入的Graphic 共用黑白兩種顏色,位置信息存儲在外部。
7.效果
- 存儲節約,和以下因素有關:
- 1.因為共用,實例總數減少的數目。
- 2.對象內部狀態的平均數目。
- 3.外部狀態是計算的還是存儲的。