#include <iostream>
using namespace std;
// FlyWeight 디자인패턴: 작은객체를 공유하는 기법
//               동일한 상태를 가지는 객체는 메모리에 한번만 놓이게 한다.
//               Factory를 사용하게 된다. 또한 Factory는 주로 Sigletone이 된다.
// 객체를 만드는 클래스를 만들어 주자.(생성자를 private로 숨기는 대신)
class BigChar
{
       char c;
       BigChar( char a ) : c (a) { }
public:
       void Show() const { cout << "[[[[ " << c << " ]]]]" << endl; }
       
       friend class BigCharFactory;
};
#include <map>
// BigChar를 생성하는 공장을 만들자.(오직 한 개.싱글톤)
class BigCharFactory
{
       map<char, BigChar*> pool;
       BigCharFactory() {}
public:
       static BigCharFactory& GetInstance()
       {
             static BigCharFactory _instance;
             return _instance;
       }
       BigChar* CreateBigChar( char c )
       {
             if ( pool[c] == 0 )
                    pool[c] = new BigChar(c);
             return pool[c];
       }
       void Reset() // 또는 Clean()
       {
             // map에 있는 모든 객체를 제거한다.
       }
       void Remove( char c )
       {
       }
};
// Helper Macro 함수
BigChar* CreateChar( char c )
{
       return BigCharFactory::GetInstance().CreateBigChar(c);
}
void foo()
{
       BigChar*  p = CreateChar('c');
       cout << p << endl;
}
void main()
{
       BigChar* p = CreateChar('c');
       cout << p << endl;
       foo();
}