구조패턴-Bridge

2008. 8. 18. 17:38 from 셈말짓기/GoF

-----------------------------------------------------------------------------
[Bridge]
의도:
구현과 추상화 개념을 분리하려는 것이다. 이로써 구현 자체도 하나의 추상화 개념으로 다양한
변형이 가능해지고, 구현과 독립적으로 인터페이스도 다양함을 가질 수 있게 된다.

다른 이름:
Handle/body
-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//
// A Structural part of GoF's Design Patterns
//
// - Bridge
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>

#include <iostream>
#include <memory>


/////////////////////////////////////////////////////////////////////////////
//
// Bridge
//
/////////////////////////////////////////////////////////////////////////////
class Implementor
{
public:
 virtual ~Implementor () {}
public:
 virtual void OperationImp (void) = 0;
};

class ConcreteImplementorA : public Implementor
{
public:
 virtual void OperationImp (void)
 {
  std::cout<< "ConcreteImplementorA::OperationImp()" << std::endl;
 }
};

class ConcreteImplementorB : public Implementor
{
public:
 virtual void OperationImp (void)
 {
  std::cout<< "ConcreteImplementorB::OperationImp()" << std::endl;
 }
};

class ImplementorFactory
{
private:
 ImplementorFactory(void) : Env_(0) {};
public:
 static ImplementorFactory* Instance (void)
 {
  if (0==Instance_)
  {
   Instance_ = new ImplementorFactory();
  }

  return Instance_;
 }

private:
 static ImplementorFactory* Instance_;

public:
 Implementor* CreateImplementor (void)
 {
  Implementor* p;
 
  if (Env_==0)
   p = new ConcreteImplementorA();
  else
   p = new ConcreteImplementorB();

  return p;
 }

public:
 void SetEnv (int e)
 {
  Env_ = e;
 }

 int  GetEnv (void)
 {
  return Env_;
 }

private:
 int Env_;
};

ImplementorFactory* ImplementorFactory::Instance_ = 0;

class Abstraction
{
public:
 Abstraction () : Imp_(0)
 {
 }

public:
 virtual void Operation (void)
 {
  GetImp ()->OperationImp();
 }

private:
 Implementor* GetImp (void)
 {
  if (Imp_==0)
  {
   Imp_ = ImplementorFactory::Instance()->CreateImplementor ();
  }

  return Imp_;
 }

private:
 Implementor *Imp_;
};

class RefinedAbstraction : public Abstraction
{
public:
 virtual void RefinedOperation (void)
 {
  Abstraction::Operation();
  std::cout<< "RefinedAbstraction::RefinedOperation()" << std::endl;
 }
};


/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
 std::auto_ptr<RefinedAbstraction> p;


 p.reset ( new RefinedAbstraction() );
 p->RefinedOperation ();
 p.reset ();

 std::cout << std::endl;
 ImplementorFactory::Instance()->SetEnv (1);

 p.reset ( new RefinedAbstraction() );
 p->RefinedOperation ();
 p.reset ();

 return 0;
}
-----------------------------------------------------------------------------
ConcreteImplementorA::OperationImp()
RefinedAbstraction::RefinedOperation()

ConcreteImplementorB::OperationImp()
RefinedAbstraction::RefinedOperation()

Posted by 셈말짓기 :