-----------------------------------------------------------------------------
[Factory Method]
의도:
객체를 생성하기 위해 인터페이스를 정의하지만, 어떤 클래스의 인스턴스를 생성할지에 대한
결정은 서브클래스에서 이루어지도록 Factory Method 패턴은 서브클래스에게 인스턴스 생성의
책임을 미룬다.
다른 이름:
Virtual Constructor
-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//
// A creational part of GoF's Design Patterns
//
// - Factory Method
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <memory>
/////////////////////////////////////////////////////////////////////////////
//
// Product
//
//
/////////////////////////////////////////////////////////////////////////////
class Product
{
public:
Product() {}
virtual ~Product() {}
public:
virtual void Operation (void) = 0;
};
class ConcreteProductA : public Product
{
public:
ConcreteProductA(){}
virtual ~ConcreteProductA() {}
public:
virtual void Operation (void)
{
std::cout << "ConcreteProductA::Operation()" << std::endl;
}
};
class ConcreteProductB : public Product
{
public:
ConcreteProductB(){}
virtual ~ConcreteProductB() {}
public:
virtual void Operation (void)
{
std::cout << "ConcreteProductB::Operation()" << std::endl;
}
};
class ConcreteProductB_2 : public Product
{
public:
ConcreteProductB_2(){}
virtual ~ConcreteProductB_2() {}
public:
virtual void Operation (void)
{
std::cout << "ConcreteProductB_2::Operation()" << std::endl;
}
};
/////////////////////////////////////////////////////////////////////////////
//
// Factory Method
//
//
/////////////////////////////////////////////////////////////////////////////
enum ProductID
{
Id_ConcreteProductA,
Id_ConcreteProductB
};
class Createor
{
public:
virtual ~Createor() {}
public:
virtual std::auto_ptr<Product> Create (ProductID id)
{
if (Id_ConcreteProductA==id)
return std::auto_ptr<Product> ( new ConcreteProductA() );
if (Id_ConcreteProductB==id)
return std::auto_ptr<Product> ( new ConcreteProductB() );
return std::auto_ptr<Product>();
}
};
class ConcreteCreateor : public Createor
{
public:
virtual std::auto_ptr<Product> Create (ProductID id)
{
if (Id_ConcreteProductB==id)
return std::auto_ptr<Product> ( new ConcreteProductB_2() );
return Createor::Create(id);
}
};
/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
std::auto_ptr<Createor> c;
std::auto_ptr<Product> p;
std::cout << "Created by Createor()" << std::endl;
c.reset ( new Createor() );
p = c->Create ( Id_ConcreteProductA );
p->Operation ();
p = c->Create ( Id_ConcreteProductB );
p->Operation ();
std::cout << std::endl;
std::cout << "Created by ConcreteCreateor()" << std::endl;
c.reset ( new ConcreteCreateor() );
p = c->Create ( Id_ConcreteProductA );
p->Operation ();
p = c->Create ( Id_ConcreteProductB );
p->Operation ();
std::cout << std::endl;
return 0;
}
-----------------------------------------------------------------------------
Created by Createor()
ConcreteProductA::Operation()
ConcreteProductB::Operation()
Created by ConcreteCreateor()
ConcreteProductA::Operation()
ConcreteProductB_2::Operation()