Prototype
Prototype
Section titled “Prototype”One-line pattern summary
Section titled “One-line pattern summary”A pattern that creates new objects by cloning an existing prototype instead of calling a constructor.
Typical Unity use cases
Section titled “Typical Unity use cases”- When creating runtime instances by cloning monster templates.
- When quickly duplicating objects whose creation cost is high.
Parts (roles)
Section titled “Parts (roles)”- Prototype
- Clone
- Prototype Registry
Unity example (C#)
Section titled “Unity example (C#)”The code below is a simplified Unity example based on the scenario described above.
using UnityEngine;
[CreateAssetMenu(menuName = "Game/Enemy Archetype Data")]public sealed class EnemyArchetypeData : ScriptableObject{ public int baseHealth; public float moveSpeed;
public EnemyRuntimeData CloneRuntimeData() { return new EnemyRuntimeData(baseHealth, moveSpeed); }}
public sealed class EnemyRuntimeData{ public int CurrentHealth; public float CurrentMoveSpeed;
public EnemyRuntimeData(int currentHealth, float currentMoveSpeed) { CurrentHealth = currentHealth; CurrentMoveSpeed = currentMoveSpeed; }}Advantages
Section titled “Advantages”- Object creation responsibilities are well organized, which makes dependency management easier.
- Creation policies can be changed flexibly by environment or situation.
Things to watch out for
Section titled “Things to watch out for”- Avoid introducing overly abstract creation layers for simple problems.
- As creation rules increase, keeping documentation and tests in sync becomes more important.
Interaction diagram
Section titled “Interaction diagram”This shows the flow where a registered prototype is cloned and then customized with runtime values.