-----------------------------------------------------------------------------
[Abstract Factory]
의도:
구체적 클래스를 정의하지 않고도 서로 관련성이 있거나 독립적인 여러 객체의 군을 생성하기
위한 인터페이스를 제공한다.
-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//
// A creational part of GoF's Design Patterns
//
// - Abstract Factory
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <memory>
/////////////////////////////////////////////////////////////////////////////
//
// Abstract Factory
//
//
/////////////////////////////////////////////////////////////////////////////
class AbstractProdcutA
{
public:
AbstractProdcutA () {}
virtual ~AbstractProdcutA() {}
};
class AbstractProdcutB
{
public:
AbstractProdcutB () {}
virtual ~AbstractProdcutB() {}
};
class AbstractFactory
{
public:
virtual AbstractProdcutA* CreateProductA (void) = 0;
virtual AbstractProdcutB* CreateProductB (void) = 0;
};
//// ------------------------------------------------------------------------
class ProdcutA1 : public AbstractProdcutA
{
public:
ProdcutA1()
{
std::cout << "ProdcutA1()" << std::endl;
}
};
class ProdcutA2 : public AbstractProdcutA
{
public:
ProdcutA2()
{
std::cout << "ProdcutA2()" << std::endl;
}
};
class ProdcutB1 : public AbstractProdcutB
{
public:
ProdcutB1()
{
std::cout << "ProdcutB1()" << std::endl;
}
};
class ProdcutB2 : public AbstractProdcutB
{
public:
ProdcutB2()
{
std::cout << "ProdcutB2()" << std::endl;
}
};
//// ------------------------------------------------------------------------
class ConcreteFactory1 : public AbstractFactory
{
public:
virtual AbstractProdcutA* CreateProductA (void)
{
return new ProdcutA1();
}
virtual AbstractProdcutB* CreateProductB (void)
{
return new ProdcutB1();
}
};
class ConcreteFactory2 : public AbstractFactory
{
virtual AbstractProdcutA* CreateProductA (void)
{
return new ProdcutA2();
}
virtual AbstractProdcutB* CreateProductB (void)
{
return new ProdcutB2();
}
};
//// ------------------------------------------------------------------------
class Client
{
public:
void Create (AbstractFactory *f)
{
ProdcutA_.reset( f->CreateProductA() );
ProdcutB_.reset( f->CreateProductB() );
}
private:
std::auto_ptr<AbstractProdcutA> ProdcutA_;
std::auto_ptr<AbstractProdcutB> ProdcutB_;
};
/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
Client client;
std::cout << "by ConcreteFactory1" << std::endl;
client.Create ( &ConcreteFactory1() );
std::cout << std::endl;
std::cout << "by ConcreteFactory2" << std::endl;
client.Create ( &ConcreteFactory2() );
return 0;
}
-----------------------------------------------------------------------------
by ConcreteFactory1
ProdcutA1()
ProdcutB1()
by ConcreteFactory2
ProdcutA2()
ProdcutB2()