예를 들어 A Class가 B Class를 의존할 때, B Object를 A가 직접 생성하지 않고 외부에서 생성해서 넘겨주면 의존성 주입했다고 한다.
![]()
왼쪽은 A에서 B를 생성하는 의존 형태, 오른쪽은 외부에서 B 객체를 생성하고 A에게 주입하는 형태
public interface IWeapon
{
void Attack();
}
public class Sword : IWeapon
{
public void Attack() => Debug.Log("검 공격!");
}
public class Player
{
private IWeapon weapon;
public Player(IWeapon weapon) // 의존성 주입
{
this.weapon = weapon;
}
public void Attack() => weapon.Attack();
}
// 사용 예시
Player player = new Player(new Sword()); // 무기를 자유롭게 변경 가능
public class EventManager
{
public static event Action OnPlayerDeath;
public static void PlayerDied() => OnPlayerDeath?.Invoke();
}
public class UIManager
{
public UIManager() => EventManager.OnPlayerDeath += ShowGameOverScreen;
private void ShowGameOverScreen() => Debug.Log("게임 오버!");
}
public class EventBus
{
private static Dictionary<Type, Action<object>> events = new Dictionary<Type, Action<object>>();
public static void Subscribe<T>(Action<T> listener)
{
if (!events.ContainsKey(typeof(T))) events[typeof(T)] = obj => { };
events[typeof(T)] += obj => listener((T)obj);
}
public static void Publish<T>(T message)
{
if (events.ContainsKey(typeof(T))) events[typeof(T)](message);
}
}
// 사용 예시
public class Enemy
{
public void TakeDamage(int damage) => EventBus.Publish(new DamageEvent(damage));
}
public class UIManager
{
public UIManager() => EventBus.Subscribe<DamageEvent>(OnDamageReceived);
private void OnDamageReceived(DamageEvent e) => Debug.Log($"체력 감소: {e.damage}");
}
public class ServiceLocator
{
private static Dictionary<Type, object> services = new Dictionary<Type, object>();
public static void Register<T>(T service) => services[typeof(T)] = service;
public static T Get<T>() => (T)services[typeof(T)];
}
// 사용 예시
ServiceLocator.Register<IWeapon>(new Sword());
IWeapon weapon = ServiceLocator.Get<IWeapon>();
weapon.Attack();
public interface ICommand
{
void Execute();
}
public class JumpCommand : ICommand
{
public void Execute() => Debug.Log("점프 실행!");
}
public class InputHandler
{
private ICommand command;
public void SetCommand(ICommand command) => this.command = command;
public void ExecuteCommand() => command?.Execute();
}
// 사용 예시
InputHandler inputHandler = new InputHandler();
inputHandler.SetCommand(new JumpCommand());
inputHandler.ExecuteCommand();
public interface IAttackStrategy
{
void Attack();
}
public class SwordAttack : IAttackStrategy
{
public void Attack() => Debug.Log("검 공격!");
}
public class Player
{
private IAttackStrategy attackStrategy;
public void SetStrategy(IAttackStrategy strategy) => attackStrategy = strategy;
public void Attack() => attackStrategy?.Attack();
}
// 사용 예시
Player player = new Player();
player.SetStrategy(new SwordAttack());
player.Attack();
public interface IState
{
void Handle();
}
public class IdleState : IState
{
public void Handle() => Debug.Log("대기 상태");
}
public class Player
{
private IState state;
public void SetState(IState newState) => state = newState;
public void Update() => state?.Handle();
}
// 사용 예시
Player player = new Player();
player.SetState(new IdleState());
player.Update();