Prototype
TypeScriptApplicationPrototype 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");