콘텐츠로 이동

ChainOfResponsibility

요청을 여러 처리자 체인에 통과시키며 각 처리자가 순차적으로 책임을 수행하는 패턴입니다.

  • 데미지 계산에 보호막/버프/저항을 순차 적용할 때
  • 입력 필터를 단계별 처리할 때
  • Handler
  • Concrete Handler
  • Next Handler

아래 코드는 위에서 설명한 대표 상황을 Unity 프로젝트 맥락으로 단순화한 예시입니다.

public abstract class DamageModifierHandler
{
private DamageModifierHandler nextHandler;
public DamageModifierHandler SetNext(DamageModifierHandler nextHandler)
{
this.nextHandler = nextHandler;
return nextHandler;
}
public int Handle(int incomingDamage)
{
int updatedDamage = ModifyDamage(incomingDamage);
return nextHandler == null ? updatedDamage : nextHandler.Handle(updatedDamage);
}
protected abstract int ModifyDamage(int incomingDamage);
}
public sealed class ShieldDamageHandler : DamageModifierHandler
{
protected override int ModifyDamage(int incomingDamage)
{
return System.Math.Max(0, incomingDamage - 20);
}
}
  • 행동 로직을 분리해 변경 영향도를 줄일 수 있습니다.
  • 규칙 추가/교체가 비교적 안전합니다.
  • 객체 수와 간접 호출이 늘어 흐름 파악이 어려워질 수 있습니다.
  • 전환/실행 순서 버그를 테스트로 고정해야 합니다.

요청을 체인으로 넘기며 처리 가능 핸들러가 책임지는 흐름입니다.

Chain Of Responsibility 흐름