'Design Patterns'에 해당되는 글 13건

  1. 2013.07.02 POSA1 Layers Architecture pttern
  2. 2011.12.13 Bridge Patten (브릿지 패턴)
  3. 2011.12.06 Singleton (싱글턴)
  4. 2011.12.05 Prototype
  5. 2011.11.16 Factory Method
Design Patterns2013. 7. 2. 16:01

 

레이어 아키텍쳐 패턴 이다.

 

POSA1 책에는 OSI Network 7계층을 예제로 설명이 되어 있다. 적절한 예인듯 하다.

 

레이어 패턴은 약간의 실무 경험이 있다면 자신의 경험에 비추어 비교적 이해가 쉬울것 같다. 이렇게 사용하고 있는 예가 많기 때문이다.

 

예를 들면, 보통 처음 ADO 를 배울때, MSDN 샘플을 보면 DB 에 Insert 할 데이타를 바로 가공하고 바로 Execute 해서 DB 에 데이타를 추가 한다. (로직 처리나 DB 에 데이타를 읽거나 쓰는 부분이 하나의 Layer 에 존재한다.)

 

그러나, 우리는 실무에서 그렇게 사용하지 않는다. ADO 를 이용해서 DB 에 데이타를 읽거나 쓰는 작업은 따로 소스를 분리하거나, 라이브러리, DLL 형태로 제작하고 사용 한다.

 

ADO 를 사용해서 DB 에 데이타를 읽거나 쓰는 Layer 와 실제 데이타를 Server 나 Application 에서 사용하는 로직 Layer 를 구분한다.

 

Layer 들은 서로 독립적이어야 하며 Interface 를 통해 서로 참조 해야 한다. 그래야 Layer 가 변경되었을 때도 유연하게 대처 할 수 있다. (ADO 가 ODBC 나 MYSQL 로 변경된다고 해도 해당 레이어만 변경 적용할 수 있다.)

 

'Design Patterns' 카테고리의 다른 글

Bridge Patten (브릿지 패턴)  (0) 2011.12.13
Singleton (싱글턴)  (0) 2011.12.06
Prototype  (0) 2011.12.05
Factory Method  (0) 2011.11.16
Builder  (0) 2011.11.15
Posted by 상현달
Design Patterns2011. 12. 13. 23:01

구조 패턴이다. 구현부를 객체를 추상화 함으로서 분리 시킨다.


Abstraction = IMonster
RefinedAbstraction = FireMonster = IceMonster
Implementor = IAttackType
ConcreateImplementorA = FireAttack
ConcreateImplementorB = IceAttack

예제로 보면 새로운 몬스터가 생겨날때 마다 RefinedAbstraction, IAttackType 의 class 가 증가 하게 된다.
몬스터 타입과(IMonster) 공격타입(IAttackType)을 추상화 하였기 때문에, 추후에 몬스터가 추가 된다 하더라도 몬스터를 사용하는 코드에서는 변화가 없고 객체만 늘려주면 된다.

struct IAttackType 
{
	virtual void Attack() = 0; 
};  

struct FireAttack : public IAttackType
{    
	void Attack() { cout << "공격: 불을 뿜다 화이아~" << endl; }; 
};  

struct IceAttack : public IAttackType 
{     
	void Attack() { cout << "공격: 얼음 화살 ㄱㄱ" << endl; }; 
};  	

/////////////   
struct IMonster
{    
	void Attack() 
	{
		attack_type_->Attack();
	}	
	IAttackType* attack_type_;  	
};   

struct FireMonster : public IMonster
{     
	FireMonster() {attack_type_ = new FireAttack;};	
};  

struct IceMonster : public IMonster
{     
	IceMonster() {attack_type_ = new IceAttack;};	
};  
/////////////  	


int _tmain(int argc, _TCHAR* argv[])
{    
	IMonster* fire_monster = new FireMonster;
	IMonster* ice_monster  = new IceMonster;

	fire_monster->Attack();
	ice_monster->Attack();
	
	return 0;
}

공격: 불을 뿜다 화이아~
공격: 얼음 화살 ㄱㄱ

'Design Patterns' 카테고리의 다른 글

POSA1 Layers Architecture pttern  (0) 2013.07.02
Singleton (싱글턴)  (0) 2011.12.06
Prototype  (0) 2011.12.05
Factory Method  (0) 2011.11.16
Builder  (0) 2011.11.15
Posted by 상현달
Design Patterns2011. 12. 6. 18:08

마지막 생성 패턴이다.
이건 머 말할것도 없다. 유일 객체 생성~ 


struct MonsterManager
{
public:
    MonsterManager() {cout << "This is MonsterManager !!!" << endl;};
};

////////////

template 
class Singleton
{
public:
    static T* Instance()
    {
        if (NULL == instance_)
        {
            instance_ = new T;
        }
        return instance_;
    }

    static void Release()
    {
        if (instance_) 
        {		
            delete instance_;
            instance_ = NULL;
        }
    }

private:

    static T* instance_;
};

template< class T > T * Singleton::instance_ = NULL;

void main()
{   
    Singleton::Instance();    
    Singleton::Release();
    Singleton::Instance();    
}


This is MonsterManager !!!
This is MonsterManager !!!

'Design Patterns' 카테고리의 다른 글

POSA1 Layers Architecture pttern  (0) 2013.07.02
Bridge Patten (브릿지 패턴)  (0) 2011.12.13
Prototype  (0) 2011.12.05
Factory Method  (0) 2011.11.16
Builder  (0) 2011.11.15
Posted by 상현달
Design Patterns2011. 12. 5. 18:02

4번째 생성 패턴이다.
Builder 패턴과 Factory Method 의 혼합으로 봐도 무방할 것 같다.
Prototype class 가 factory Method 처럼 동작하고 ConcreatePrototype 가 Builder 패턴 처럼 동작 한다.

Prototype class 에서 어떤 객체를 생성할지를 추상화 하고 실제 생성 절차는 서브 class 인 ConcreatePrototype 에서 한다.


Prototype = ImonsterProtype

ConcreateProtytype1 = FireMonster_Creator
ConcreateProtytype2 = IceMonster_Creator

struct IAttackType
{    
    virtual void Attack() = 0;
};

struct FireAttack : public IAttackType
{
    void Attack() { cout << "공격: 불을 뿜다 화이아~" << endl; };
};

struct IceAttack : public IAttackType
{
    void Attack() { cout << "공격: 얼음 화살 ㄱㄱ" << endl; };
};

/////////////

struct IDefensType
{
    virtual void Defenns() = 0;
};

struct AvoidDefens : public IDefensType
{
    void Defenns() { cout << "방어: 회피 하기" << endl << endl; };
};

struct IceShieldDefens : public IDefensType
{
    void Defenns() { cout << "방어: 얼음방패 막기" << endl << endl; };
};

/////////////

struct IMonster
{
    IAttackType* attack_type_;
    IDefensType* defens_type_;
};

struct FireMonster : public IMonster
{
    
};

struct IceMonster : public IMonster
{
    
};

/////////////

struct IMonsterProtype
{
    virtual IMonster* Clone() = 0;    
};

struct FireMonster_Creator : public IMonsterProtype
{
    IMonster* Clone()
    {
        FireMonster* monster = new FireMonster;
        monster->attack_type_ = new FireAttack;
        monster->defens_type_ = new AvoidDefens;
        return monster;
    }    
};

struct IceMonster_Creator : public IMonsterProtype
{
    IMonster* Clone()
    {
        IceMonster* monster = new IceMonster;
        monster->attack_type_ = new IceAttack;
        monster->defens_type_ = new IceShieldDefens;
        return monster;
    }    
};

////////////

void Operation(IMonster* monster)
{
    monster->attack_type_->Attack();
    monster->defens_type_->Defenns();
}

void main()
{   
    IMonsterProtype* FireMonster_Prototype = new FireMonster_Creator;
    IMonsterProtype* IceMonster_Prototype = new IceMonster_Creator;

    IMonster* fire_monster = FireMonster_Prototype->Clone();
    IMonster* ice_monseter  = IceMonster_Prototype->Clone();  

    Operation(fire_monster);
    Operation(ice_monseter);
    
}
/*
공격: 불을 뿜다 화이아~
방어: 회피 하기
공격: 얼음 화살 ㄱㄱ
방어: 얼음방패 막기
*/

'Design Patterns' 카테고리의 다른 글

Bridge Patten (브릿지 패턴)  (0) 2011.12.13
Singleton (싱글턴)  (0) 2011.12.06
Factory Method  (0) 2011.11.16
Builder  (0) 2011.11.15
Abstract Factory  (0) 2011.11.15
Posted by 상현달
Design Patterns2011. 11. 16. 21:21

Abstract Factory, Builder 패턴은 객체에 대한 구성 요소에 대한 생성을 추상화 시켜서 객체를 완성 시켜 나가는 방식 이었지만, Factory Method 는 객체 자체의 생성 부분을 추상화 시키는(서브 클래스가 생성) 것이다. 간단 깔끔하다.

 



Creator = ICreator
ConcreateCreator = FireMonster_Creator, IceMonster_Creator

Product = IMonster
ConcreateProduct = FireMonster, IceMonster


struct IAttackType
{    
    virtual void Attack() = 0;
};

struct FireAttack : public IAttackType
{
    void Attack() { cout << "공격: 불을 뿜다 화이아~" << endl; };
};

struct IceAttack : public IAttackType
{
    void Attack() { cout << "공격: 얼음 화살 ㄱㄱ" << endl; };
};

/////////////

struct IDefensType
{
    virtual void Defenns() = 0;
};

struct AvoidDefens : public IDefensType
{
    void Defenns() { cout << "방어: 회피 하기" << endl << endl; };
};

struct IceShieldDefens : public IDefensType
{
    void Defenns() { cout << "방어: 얼음방패 막기" << endl << endl; };
};

/////////////

struct IMonster
{
    IAttackType* attack_type_;
    IDefensType* defens_type_;
};

struct FireMonster : public IMonster
{
    FireMonster()
    {
        attack_type_ = new FireAttack;
        defens_type_ = new AvoidDefens;
    }
};

struct IceMonster : public IMonster
{
    IceMonster()
    {
        attack_type_ = new IceAttack;
        defens_type_ = new IceShieldDefens;
    }
};

/////////////

struct ICreator
{
    virtual IMonster* CreateMonster() = 0;
    virtual void Operation()
    {
        IMonster* monsetr = CreateMonster();
        monsetr->attack_type_->Attack();
        monsetr->defens_type_->Defenns();
    }
};

struct FireMonster_Creator : public ICreator
{
    IMonster* CreateMonster() 
    {
        return new FireMonster;
    }    
};

struct IceMonster_Creator : public ICreator
{
    IMonster* CreateMonster() 
    {
        return new IceMonster;
    } 
};

////////////

void main()
{   
    ICreator* fire_monster = new FireMonster_Creator;
    fire_monster->Operation();

    ICreator* ice_monster = new IceMonster_Creator;
    ice_monster->Operation();
}

공격: 불을 뿜다 화이아~
방어: 회피 하기

공격: 얼음 화살 ㄱㄱ
방어: 얼음방패 막기

'Design Patterns' 카테고리의 다른 글

Singleton (싱글턴)  (0) 2011.12.06
Prototype  (0) 2011.12.05
Builder  (0) 2011.11.15
Abstract Factory  (0) 2011.11.15
객체지향 설계 원칙 5-5 ISP (Interface Segregation Principle)  (2) 2008.07.08
Posted by 상현달