Skip to content

Prototype

A pattern that creates new objects by cloning an existing prototype instead of calling a constructor.

  • When creating runtime instances by cloning monster templates.
  • When quickly duplicating objects whose creation cost is high.
  • Prototype
  • Clone
  • Prototype Registry

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;
}
}
  • Object creation responsibilities are well organized, which makes dependency management easier.
  • Creation policies can be changed flexibly by environment or situation.
  • Avoid introducing overly abstract creation layers for simple problems.
  • As creation rules increase, keeping documentation and tests in sync becomes more important.

This shows the flow where a registered prototype is cloned and then customized with runtime values.

Prototype Flow