콘텐츠로 이동

Prototype

새 객체를 생성자 호출 대신 기존 원형 복제로 만드는 패턴입니다.

  • 몬스터 템플릿 복제로 런타임 인스턴스를 만들 때
  • 생성 비용이 높은 객체를 빠르게 복제할 때
  • Prototype
  • Clone
  • Prototype Registry

아래 코드는 위에서 설명한 대표 상황을 Unity 프로젝트 맥락으로 단순화한 예시입니다.

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;
}
}
  • 객체 생성 책임을 정리해 의존성 관리가 쉬워집니다.
  • 환경별/상황별 생성 정책을 유연하게 바꿀 수 있습니다.
  • 간단한 문제에 과한 생성 추상화를 넣지 않아야 합니다.
  • 생성 규칙이 많아질수록 문서와 테스트 동기화가 중요합니다.

등록된 원형을 복제하고 런타임 값만 덧입혀 인스턴스를 만드는 흐름입니다.

Prototype 흐름