Skip to content

Composite

A tree-structure pattern that treats individual objects and composed objects through the same interface.

  • When building quest objectives as a tree.
  • When handling node and group node objects in the same way.
  • Component
  • Leaf
  • Composite

The code below is a simplified Unity example based on the scenario described above.

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;
}
}
  • It clarifies module boundaries and reduces coupling.
  • Features can be extended or integrated without modifying existing code.
  • If wrapper layers become too deep, debugging gets harder.
  • Interfaces should stay small so responsibility boundaries do not blur.

This shows the recursive flow where both single objects and composites are handled through the same interface.

Composite Flow