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 상현달