FLYWEIGHT 플라이급 패턴 (분동 패턴)

  • 공유를 통해 많은 작은 객체들을 지원 한다.
  • 메모리 절약에 용의 하다.
  • BTree , map 이용


#include <iostream>

#include <map> using namespace std; class Flyweight{ public: virtual void operation() = 0; }; class UnsharedConcreteFlyweight : public Flyweight{ public: void operation() override { cout << "Unshared" << endl; } }; class ConcreteFlyweight : public Flyweight{ public: void operation() override { cout << "Share" << endl; } }; class FlyweightFactory{ public: Flyweight* getFlyweight(int key){ if(_map.find(key) == _map.end()){ _map[key] = new ConcreteFlyweight; } return _map[key]; } private: map<int,Flyweight*> _map; }; int main(){ FlyweightFactory factory; Flyweight* _flyweight = factory.getFlyweight(1); _flyweight->operation(); return 0; }


+ Recent posts