意图:运用共享技术有效地支持大量细粒度的对象。
class Flyweight { public: Flyweight() {} virtual ~Flyweight() {} virtual void Operation(int extrinsic_state) = 0; }; class ConcreteFlyweight: public Flyweight { public: ConcreteFlyweight(const string& key) { this->key = key; } ~ConcreteFlyweight() {} void Operation(int extrinsic_state) { cout<<"ConcreteFlyweight::Operation()"<<endl; } private: string key; }; class FlyweightFactory { public: FlyweightFactory() {} ~FlyweightFactory() {} Flyweight* GetFlyweight(const string& key) { if(flyweightMap.find(key) == flyweightMap.end()) { flyweightMap[key] = new ConcreteFlyweight(key); } return flyweightMap[key]; } private: map<string, Flyweight*> flyweightMap; };