Skip to content

TemplateMethod

A pattern where the overall procedure is fixed, while only the detailed steps are changed in subclasses.

  • When the skill execution order is the same but the effect differs.
  • When common pre-processing and post-processing must be enforced.
  • Template Method
  • Primitive Operation
  • Concrete Class

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

public abstract class SkillExecutionTemplate
{
public void Execute()
{
if (!CanExecute())
{
return;
}
ConsumeCost();
PlayCastAnimation();
ApplyEffect();
}
protected virtual bool CanExecute() => true;
protected virtual void ConsumeCost() { }
protected virtual void PlayCastAnimation() { }
protected abstract void ApplyEffect();
}
public sealed class HealSkillTemplate : SkillExecutionTemplate
{
protected override void ApplyEffect()
{
// Heal target.
}
}
  • Behavior is separated into smaller units, which reduces the impact of changes.
  • Adding or swapping rules is relatively safe.
  • As the number of objects and indirect calls increases, the flow can become harder to follow.
  • Ordering bugs should be pinned down with tests.

This shows the flow where the parent fixes the algorithm order and the subclass changes only some steps.

Template Method Flow