말 그대로 이다 생성을 추상화 시키는 패턴
사용 하기 위해 생성할 여러 객체들(ProductA1 ~ ProductB2) 을 선택적으로 한꺼번에 생성 가능하고, 그 부분을 추상화 한다.
AbstractFactory = IMonsterTypeFactory
ConcreateFactory1 = FireSoulFactory
ConcreateFactory2 = IceSoulFactory
AbstractProductA = IAttackType
ProductA1 = FireAttack
ProductA2 = IceAttack
AbstractProductB = IDefensType
ProductB1 = AvoidDefens
ProductB2 = iceShieldDefens
사용 하기 위해 생성할 여러 객체들(ProductA1 ~ ProductB2) 을 선택적으로 한꺼번에 생성 가능하고, 그 부분을 추상화 한다.
AbstractFactory = IMonsterTypeFactory
ConcreateFactory1 = FireSoulFactory
ConcreateFactory2 = IceSoulFactory
AbstractProductA = IAttackType
ProductA1 = FireAttack
ProductA2 = IceAttack
AbstractProductB = IDefensType
ProductB1 = AvoidDefens
ProductB2 = iceShieldDefens
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 IMonsterTypeFactory { virtual IAttackType* CreateAttackType() = 0; virtual IDefensType* CreateDefensType() = 0; }; struct FireSoulFactory : public IMonsterTypeFactory { IAttackType* CreateAttackType() { return new FireAttack; }; IDefensType* CreateDefensType() { return new AvoidDefens; }; }; struct IceSoulFactory : public IMonsterTypeFactory { IAttackType* CreateAttackType() { return new IceAttack; }; IDefensType* CreateDefensType() { return new IceShieldDefens; }; }; ///////////// struct IMonster { IMonster(IMonsterTypeFactory* monster) { attack_type_ = monster->CreateAttackType(); defens_type_ = monster->CreateDefensType(); }; IAttackType* attack_type_; IDefensType* defens_type_; }; struct FireMonster : public IMonster { FireMonster() : IMonster(new FireSoulFactory) {}; }; struct IceMonster : public IMonster { IceMonster() : IMonster(new IceSoulFactory) {}; }; ///////////// void MonsterAction(IMonsterTypeFactory* monster) { IAttackType* attack_type = monster->CreateAttackType(); IDefensType* defens_type = monster->CreateDefensType(); attack_type->Attack(); defens_type->Defenns(); } //////////// void main() { FireMonster fire_monster; fire_monster.attack_type_->Attack(); fire_monster.defens_type_->Defenns(); IceMonster ice_monster; ice_monster.attack_type_->Attack(); ice_monster.defens_type_->Defenns(); }
공격: 불을 뿜다 화이아~
방어: 회피 하기
공격: 얼음 화살 ㄱㄱ
방어: 얼음방패 막기
'Design Patterns' 카테고리의 다른 글
Factory Method (0) | 2011.11.16 |
---|---|
Builder (0) | 2011.11.15 |
객체지향 설계 원칙 5-5 ISP (Interface Segregation Principle) (2) | 2008.07.08 |
객체지향 설계 원칙 5-4 DIP (Dependency Inversion Principle) (0) | 2008.06.28 |
객체지향 설계 원칙 5-3 SRP (Single Responsibility Principle) (0) | 2008.06.21 |