-----------------------------------------------------------------------------
[Builder]
-----------------------------------------------------------------------------
복잡한 객체를 생성하는 방법과 표현하는 방법을 정의하는 클래스를 별도로 분리하여 서로 다른
표현이라도 이를 생성할 수 있는 동일한 구축 공정을 제공할 수 있도록 한다.
-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//
// A creational part of GoF's Design Patterns
//
// - Builder
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <memory>
/////////////////////////////////////////////////////////////////////////////
//
// A creational part of GoF's Design Patterns
//
// - Builder
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <memory>
/////////////////////////////////////////////////////////////////////////////
//
// Builder
//
//
/////////////////////////////////////////////////////////////////////////////
class Product
{
public:
Product() :
PartA_(0),
PartB_(0),
PartC_(0)
{
}
virtual ~Product()
{
}
public:
void SetPartA (int a) { PartA_= a; }
void SetPartB (int b) { PartB_= b; }
void SetPartC (int c) { PartC_= c; }
void Operation (void)
{
std::cout << "[Product]" << std::endl
<< "PartA=" << PartA_ << std::endl
<< "PartB=" << PartB_ << std::endl
<< "PartC=" << PartC_ << std::endl
<< std::endl;
}
private:
int PartA_;
int PartB_;
int PartC_;
};
//// ------------------------------------------------------------------------
class AbstractBuilder
{
public:
virtual ~AbstractBuilder() {}
public:
std::auto_ptr<Product> GetResult (void)
{
return Product_;
}
void NewProduct (void)
{
Product_.reset ( new Product() );
}
public:
virtual void BuildPartA (void)=0;
virtual void BuildPartB (void)=0;
virtual void BuildPartC (void)=0;
protected:
std::auto_ptr<Product> Product_;
};
class ConcreteBuilderA : public AbstractBuilder
{
public:
virtual void BuildPartA (void) { Product_->SetPartA(1); }
virtual void BuildPartB (void) { Product_->SetPartB(2); }
virtual void BuildPartC (void) { Product_->SetPartC(3); }
};
class ConcreteBuilderB : public AbstractBuilder
{
public:
virtual void BuildPartA (void) { Product_->SetPartA(4); }
virtual void BuildPartB (void) { Product_->SetPartB(5); }
virtual void BuildPartC (void) { Product_->SetPartC(6); }
};
//// ------------------------------------------------------------------------
class Director
{
public:
Director() : Builder_(0) {}
virtual ~Director() {}
public:
void SetBuilder (AbstractBuilder* b)
{
Builder_ = b;
}
void Construct (void)
{
Builder_->NewProduct ();
Builder_->BuildPartA ();
Builder_->BuildPartB ();
Builder_->BuildPartC ();
}
private:
AbstractBuilder* Builder_;
};
/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
Director director;
ConcreteBuilderA builder_A;
ConcreteBuilderB builder_B;
std::auto_ptr<Product> product;
director.SetBuilder (&builder_A);
director.Construct ();
product = builder_A.GetResult();
product->Operation();
director.SetBuilder (&builder_B);
director.Construct ();
product = builder_B.GetResult();
product->Operation();
return 0;
}
-----------------------------------------------------------------------------
[Product]
PartA=1
PartB=2
PartC=3
[Product]
PartA=4
PartB=5
PartC=6