예제코드
using System;
using System.Collections.Generic;
namespace FactoryMethodEx
{
public enum EnemyType
{
Zombie,
Slime
}
abstract class Enemy
{
protected EnemyType type;
protected string name;
protected int hp;
protected int exp;
protected void Attack() { }
}
class Zombie : Enemy
{
public Zombie()
{
type = EnemyType.Zombie;
name = "Zombie";
hp = 100;
exp = 50;
Console.WriteLine("{0} : 출현!!", this.name);
}
}
class Slime : Enemy
{
public Slime()
{
type = EnemyType.Slime;
name = "Slime";
hp = 200;
exp = 15;
Console.WriteLine("{0} : 출현!!", this.name);
}
}
abstract class EnemyGenerator
{
private List<Enemy> _enemy = new List<Enemy>();
public EnemyGenerator()
{
}
public List<Enemy> Enemys
{
get { return _enemy; }
}
public abstract void CreateEnemys(); // Factory Method
}
class PatternAGenerator : EnemyGenerator
{
public override void CreateEnemys()
{
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
}
}
class PatternBGenerator : EnemyGenerator
{
public override void CreateEnemys()
{
Enemys.Add(new Slime());
Enemys.Add(new Slime());
Enemys.Add(new Slime());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
Enemys.Add(new Zombie());
}
}
class Program
{
static void Main(string[] args)
{
EnemyGenerator[] enemyGenerators = new EnemyGenerator[2];
enemyGenerators[0] = new PatternAGenerator();
enemyGenerators[1] = new PatternBGenerator();
enemyGenerators[1].CreateEnemys();
Console.ReadKey();
}
}
}
'Programming > Design Pattern' 카테고리의 다른 글
[C# 디자인패턴] 제6강 프로토타입패턴 (0) | 2015.07.18 |
---|---|
[C# 디자인패턴] 제5강 빌더패턴 (0) | 2015.07.18 |
[C# 디자인패턴] 제4강 추상팩토리패턴 (0) | 2015.07.18 |
[C# 디자인패턴] 제2강 싱글턴패턴 (0) | 2015.07.18 |
디자인패턴 개론 (0) | 2015.07.18 |