Creational design patterns abstract the instantiation process. The Factory Method defines an interface for creating an object while letting subclasses decide which class to instantiate. The Builder pattern constructs complex objects step-by-step with method chaining, avoiding telescoping constructors. Singleton ensures a class has only one instance with global access.
A custom burger kitchen: instead of forcing customers into the kitchen to assemble raw patties and sauces, the customer specifies preferences to a Builder ("addCheese()", "doublePatty()", "build()"), which constructs the exact burger cleanly.
Hold the picture while you read the contract above and the moves below; the analogy is what makes the abstraction checkable when the class diagram gets large.
Identify complex constructors with 5+ arguments or branching instantiation logic
Encapsulate object creation inside dedicated Factory classes or fluent Builder interfaces
Define return types as common abstract interfaces
Builder pattern validates required attributes before returning immutable instance via build()
Clients invoke factory methods without knowing underlying concrete classes
// Fluent Builder Pattern with Type Validation
export class HttpRequest {
public readonly url: string;
public readonly method: 'GET' | 'POST' | 'PUT' | 'DELETE';
public readonly headers: Record<string, string>;
public readonly body?: string;
constructor(builder: HttpRequestBuilder) {
this.url = builder.url;
this.method = builder.method;
this.headers = builder.headers;
this.body = builder.body;
}
}
export class HttpRequestBuilder {
public url: string = '';
public method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET';
public headers: Record<string, string> = {};
public body?: string;
setUrl(url: string) { this.url = url; return this; }
setMethod(method: 'GET' | 'POST' | 'PUT' | 'DELETE') { this.method = method; return this; }
addHeader(k: string, v: string) { this.headers[k] = v; return this; }
setBody(body: string) { this.body = body; return this; }
build(): HttpRequest {
if (!this.url) throw new Error('URL is required');
return new HttpRequest(this);
}
}Fluent Builders enforce immutability by returning frozen objects, preventing partial-state corruption in concurrent environments.