콘텐츠로 이동

Composite

개별 객체와 복합 객체를 동일 인터페이스로 다루는 트리 구조 패턴입니다.

  • 퀘스트 목표를 트리로 구성할 때
  • 노드/그룹 노드를 같은 방식으로 처리할 때
  • Component
  • Leaf
  • Composite

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

using System.Collections.Generic;
public interface IQuestCondition
{
bool IsCompleted();
}
public sealed class KillMonsterCondition : IQuestCondition
{
public bool IsCompleted() => false;
}
public sealed class AllConditionsGroup : IQuestCondition
{
private readonly List<IQuestCondition> childConditions = new();
public void Add(IQuestCondition childCondition)
{
childConditions.Add(childCondition);
}
public bool IsCompleted()
{
foreach (IQuestCondition childCondition in childConditions)
{
if (!childCondition.IsCompleted())
{
return false;
}
}
return true;
}
}
  • 모듈 경계를 명확히 해 결합도를 낮출 수 있습니다.
  • 기존 코드 수정 없이 기능 확장/통합이 쉬워집니다.
  • 래퍼/어댑터 계층이 깊어지면 디버깅이 어려워집니다.
  • 책임 경계가 흐려지지 않도록 인터페이스를 작게 유지해야 합니다.

개별 객체와 복합 객체를 동일 인터페이스로 재귀 처리하는 흐름입니다.

Composite 흐름