#include #include #include using namespace std; class Flyweight{ public: virtual void Operation(int extrinsicstate)=0; }; class ConcreteFlyweight:public Flyweight{ public: int intrinsicState=10; void Operation(int extrinsicstate){ cout<<"ConcreteFlyweight: "< FlyweightRepository; class FlyweightFactory{ static FlyweightRepository flyweights; public: FlyweightFactory(){ static ConcreteFlyweight x; static ConcreteFlyweight y; static ConcreteFlyweight z; flyweights["X"] = &x; flyweights["Y"] = &y; flyweights["Z"] = &z; } Flyweight& operator[](string i) { return *flyweights[i]; } }; FlyweightRepository FlyweightFactory::flyweights; static FlyweightFactory f; main(){ //Arbitrary extrinsic state int extrinsicstate = 22; //Work with different flyweight instances Flyweight& fx = f["X"]; fx.Operation(--extrinsicstate); Flyweight& fy = f["Y"]; fy.Operation(--extrinsicstate); Flyweight& fz = f["Z"]; fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight uf; uf.Operation(--extrinsicstate); return 0; } /*=================== OUTPUT =================== ConcreteFlyweight: 21 ConcreteFlyweight: 20 ConcreteFlyweight: 19 UnsharedConcreteFlyweight: 18 =================================================*/