팩토리 패턴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public abstract class Enemy
{
    // Common enemy behavior and properties
}
 
 
public class Zombie : Enemy
{
    // Zombie-specific behavior and properties
}
 
 
public class Ghost : Enemy
{
    // Ghost-specific behavior and properties
}
 
 
public static class EnemyFactory
{
    public static Enemy CreateEnemy(string type)
    {
        switch (type)
        {
            case "Zombie":
                return new Zombie();
            case "Ghost":
                return new Ghost();
            default:
                throw new ArgumentException("Invalid enemy type");
        }
    }
}
 
 
public class EnemySpawner : MonoBehaviour
{
    private void SpawnEnemy(string type)
    {
        Enemy enemy = EnemyFactory.CreateEnemy(type);
        // Spawn and initialize the enemy
    }
}
 
cs


데코레이터 패턴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public abstract class Weapon
{
    public abstract int GetDamage();
}
 
 
public class Sword : Weapon
{
    public override int GetDamage()
    {
        return 10;
    }
}
 
 
public abstract class WeaponDecorator : Weapon
{
    protected Weapon _weapon;
 
 
    public WeaponDecorator(Weapon weapon)
    {
        _weapon = weapon;
    }
 
 
    public override int GetDamage()
    {
        return _weapon.GetDamage();
    }
}
 
 
public class FireEnchantment : WeaponDecorator
{
    public FireEnchantment(Weapon weapon) : base(weapon) { }
 
 
    public override int GetDamage()
    {
        return base.GetDamage() + 5;
    }
}
cs


스테이트 패턴


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public interface IState
{
    void Enter();
    void Execute();
    void Exit();
}
 
 
public class IdleState : IState
{
    public void Enter() { /* Enter idle state logic */ }
    public void Execute() { /* Execute idle state logic */ }
    public void Exit() { /* Exit idle state logic */ }
}
 
 
public class MovingState : IState
{
    public void Enter() { /* Enter moving state logic */ }
    public void Execute() { /* Execute moving state logic */ }
    public void Exit() { /* Exit moving state logic */ }
}
 
 
public class StateMachine
{
    private IState _currentState;
 
 
    public void ChangeState(IState newState)
    {
        _currentState?.Exit();
        _currentState = newState;
        _currentState.Enter();
    }
 
 
    public void Update()
    {
        _currentState.Execute();
    }
}
 
cs