Subclass Sandbox
Subclass Sandbox
Section titled “Subclass Sandbox”One-line pattern summary
Section titled “One-line pattern summary”A pattern where the parent provides a safe extension API and children are restricted to implementing only within that boundary.
Typical Unity use cases
Section titled “Typical Unity use cases”- When extending skills or AI while protecting core rules.
- When you want to control designer-facing extension points.
Parts (roles)
Section titled “Parts (roles)”- Base Class: provides a safe API
- Hook: child implementation point
- Subclass: extension implementation
Unity example (C#)
Section titled “Unity example (C#)”The code below is a simplified Unity example based on the scenario described above.
public abstract class SkillBase{ public void ExecuteSkill() { if (!CanExecute()) { return; }
ConsumeResource(); PlayCastAnimation(); ApplyEffect(); }
protected virtual bool CanExecute() => true; protected virtual void ConsumeResource() { } protected virtual void PlayCastAnimation() { } protected abstract void ApplyEffect();}
public sealed class FireballSkill : SkillBase{ protected override void ApplyEffect() { // Spawn fireball projectile. }}Advantages
Section titled “Advantages”- Only the API allowed by the parent is exposed, so extension code can be safely constrained.
- It is useful when common rules such as cooldowns or resource costs must be enforced in the base class.
Things to watch out for
Section titled “Things to watch out for”- If the base class grows too large, extension flexibility can actually decrease.
- If hook contracts are unclear, behavior consistency can break between subclasses.
Interaction diagram
Section titled “Interaction diagram”This shows the flow where the parent template enforces common rules and the child implements only the hook.