builder
Construct complex objects step by step, separating construction from representation.
SKILL.mdskills/code-quality/builder/SKILL.md
GitHubBuilder
Separates the construction of a complex object from its representation so the same construction process can produce different results. Eliminates "telescoping constructors".
When to Use
- Object construction requires many optional/configurable parameters
- The same construction steps must produce different representations
- You need step-by-step construction with validation at each step
- Constructors with 4+ parameters (especially many of the same type)
How to Apply
- Define the
Productclass (the complex object) - Create a
Builderinterface declaring steps for each part - Implement one or more concrete
Builderclasses - Optionally add a
Directorclass to encode common construction sequences - Client uses the builder (via director or directly) and calls
build()to get the product
Example
class QueryBuilder {
private table = '';
private conditions: string[] = [];
private limit?: number;
from(table: string) { this.table = table; return this; }
where(cond: string) { this.conditions.push(cond); return this; }
take(n: number) { this.limit = n; return this; }
build(): string {
let q = `SELECT * FROM ${this.table}`;
if (this.conditions.length) q += ` WHERE ${this.conditions.join(' AND ')}`;
if (this.limit) q += ` LIMIT ${this.limit}`;
return q;
}
}
const sql = new QueryBuilder()
.from('users')
.where('active = true')
.take(10)
.build();
// SELECT * FROM users WHERE active = true LIMIT 10