← Back to scenarios
ScenarioCatalog DB

Game character clone

Clone a base character prototype to create variant enemies or allies with shared statistics and equipment.

Problem

A game must create many related character variants efficiently without repeating complex initialization or requiring spawning code to know each concrete character class.

Related patterns

Implementations

Prototype

TypeScriptApplication

Prototype in TypeScript

Creates independent deployment templates by cloning a configured prototype and changing only the environment-specific values.

TypeScript
interface Prototype<T> {
  clone(): T;
}

class DeploymentTemplate implements Prototype<DeploymentTemplate> {
  constructor(
    readonly serviceName: string,
    readonly replicas: number,
    readonly labels: Record<string, string>,
  ) {}

  clone(): DeploymentTemplate {
    return new DeploymentTemplate(
      this.serviceName,
      this.replicas,
      { ...this.labels },
    );
  }

  withEnvironment(environment: string): DeploymentTemplate {
    const copy = this.clone();

    return new DeploymentTemplate(
      copy.serviceName,
      copy.replicas,
      {
        ...copy.labels,
        environment,
      },
    );
  }
}

const baseTemplate = new DeploymentTemplate(
  "billing-api",
  2,
  { owner: "payments" },
);

const productionTemplate = baseTemplate.withEnvironment("production");

Associated technologies