1. Intentions
Use shared technology to effectively support a large number of fine-grained objects.
2. Motivation
The flyweight pattern describes how to share objects so that they can be used fine-grained, without a high cost. Flyweight is a shared object that can be used in multiple scenarios (context) at the same time, and flyweight can act as a standalone object in each scene---this is no different from instances of unshared objects.
3. Applicability
- An application uses a large number of objects.
- Because of the use of a large number of objects, resulting in a large storage overhead.
- Most of the state of an object can become an external state.
- If you delete an object's external state, you can replace many group objects with a relatively small number of shared objects.
- The application does not depend on the object identity. Because flyweight objects can be shared, the identity test returns true for objects that are conceptually distinct.
4. Structure diagram
5. Code examples
//Flyweight.h#include<string>classflweightgraphicbase{ Public: Virtual voidPrintcolor () =0;protected: std::stringM_color;};classFlweightwhitegraphic: Publicflweightgraphicbase{ Public: Flweightwhitegraphic (); voidPrintcolor ();};classFlweightblackgraphic: Publicflweightgraphicbase{ Public: Flweightblackgraphic (); voidPrintcolor ();};
//FlyweigtFactory.h#include<string>#include<memory>#include<map>classflweightgraphicbase;classflyweightfactory{ Public: Std::shared_ptr<FlweightGraphicBase> getflyweight (std::stringScolor); voidSetflyweight (std::stringScolor, 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";}voidflweightwhitegraphic:: Printcolor () {std::cout<<"Color is:"<< M_color <<Std::endl;} Flweightblackgraphic::flweightblackgraphic () {M_color="Black";}voidflweightblackgraphic:: Printcolor () {std::cout<<"Color is:"<< M_color <<Std::endl;}
//FlyweightFactory.cpp#include"FlyweightFactory.h"std::shared_ptr<FlweightGraphicBase> flyweightfactory::getflyweight (std::stringScolor) {Auto ITER=M_mapflyweight.find (Scolor); if(iter = =M_mapflyweight.end ()) { returnnullptr; } returnIter->second;}voidFlyweightfactory::setflyweight (std::stringScolor, Std::shared_ptr<FlweightGraphicBase>pflyweight) {M_mapflyweight[scolor]=pflyweight;}
//Client.cpp#include"Flyweight.h"#include"FlyweightFactory.h"#include<iostream>voidInsert (std::stringScolor,intIndex,std::shared_ptr<flweightgraphicbase>pflweightgraphic) {Std::cout<<"posion"<< Index <<"Insert:"<< Scolor <<Std::endl;}intMain () {std::shared_ptr<FlyweightFactory> Pflyweightfactory (Newflyweightfactory); Std::shared_ptr<FlweightWhiteGraphic> Pflweightwhitegraphic (Newflweightwhitegraphic); Std::shared_ptr<FlweightBlackGraphic> Pflweightblackgraphic (Newflweightblackgraphic); 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. Test results
All inserted graphic in the instance code share both black and white colors, and location information is stored externally.
7. Effects
- Storage savings are related to the following factors:
- 1. The total number of instances is reduced because of sharing.
- 2. The average number of internal states of the object.
- 3. The external state is calculated or stored.
Flyweight (enjoy Yuan)--Object structure model