Zachary W. Huang
Idea: Factor out all of the logic for constructing a complex object into a common interface. This allows us to use different representations while constructing the same thing.
In contrast with Abstract Factory, this pattern typically returns one final, composite product while Abstract Factory returns families of elements part-by-part.
interface ComplexProduct {}
abstract class Builder {
abstract makeProductOne(): void;
abstract makeProductTwo(): void;
abstract create(): ComplexProduct;
}
class BuilderA extends Builder {
internal: any[] = [];
makeProductOne() {
this.internal.push(1)
}
makeProductTwo() {
this.internal.push(2)
}
create() {
return [...this.internal]
}
}
class BuilderB extends Builder {
internal: Record<string, number>;
makeProductOne() {
this.internal["1"] = 1
}
makeProductTwo() {
this.internal["2"] = 2
}
create() {
return {...this.internal}
}
}