Composite
Composite
Section titled “Composite”One-line pattern summary
Section titled “One-line pattern summary”A tree-structure pattern that treats individual objects and composed objects through the same interface.
Typical Unity use cases
Section titled “Typical Unity use cases”- When building quest objectives as a tree.
- When handling node and group node objects in the same way.
Parts (roles)
Section titled “Parts (roles)”- Component
- Leaf
- Composite
Unity example (C#)
Section titled “Unity example (C#)”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; }}Advantages
Section titled “Advantages”- It clarifies module boundaries and reduces coupling.
- Features can be extended or integrated without modifying existing code.
Things to watch out for
Section titled “Things to watch out for”- If wrapper layers become too deep, debugging gets harder.
- Interfaces should stay small so responsibility boundaries do not blur.
Interaction diagram
Section titled “Interaction diagram”This shows the recursive flow where both single objects and composites are handled through the same interface.