Day 01 Foundations

Creational Patterns

Creational patterns deal with object creation. Singleton, Factory, and Builder are the three you will use most. Today you will implement all three in TypeScript and understand when to apply each.

~1 hour Hands-on Precision AI Academy

Today’s Objective

Creational patterns deal with object creation. Singleton, Factory, and Builder are the three you will use most. Today you will implement all three in TypeScript and understand when to apply each.

Creational Patterns is one of the most important topics in Design Patterns in 5 Days. This lesson builds the foundation you need before moving to more advanced concepts — take time with each example and run the code yourself.

typescript
TYPESCRIPT
// Singleton — one instance shared across the app
class Database { private static instance: Database; private constructor(private url: string) {} static getInstance(): Database { if (!Database.instance) { Database.instance = new Database(process.env.DATABASE_URL!); } return Database.instance; } query(sql: string) { /* ... */ }
}

const db1 = Database.getInstance();
const db2 = Database.getInstance();
console.log(db1 === db2); // true
typescript
TYPESCRIPT
// Factory Method — delegate instantiation to subclasses
interface Button { render(): string; }

class PrimaryButton implements Button { render() { return '<button class="btn-primary">Click</button>'; }
}
class DangerButton implements Button { render() { return '<button class="btn-danger">Delete</button>'; }
}

function createButton(type: 'primary' | 'danger'): Button { if (type === 'primary') return new PrimaryButton(); return new DangerButton();
}
typescript
TYPESCRIPT
// Builder — construct complex objects step by step
class QueryBuilder { private table = ''; private conditions: string[] = []; private limitVal: number | null = null; from(table: string) { this.table = table; return this; } where(condition: string) { this.conditions.push(condition); return this; } limit(n: number) { this.limitVal = n; return this; } build(): string { let sql = \`SELECT * FROM \${this.table}\`; if (this.conditions.length) sql += \` WHERE \${this.conditions.join(' AND ')}\`; if (this.limitVal !== null) sql += \` LIMIT \${this.limitVal}\`; return sql; }
}

const query = new QueryBuilder() .from('users') .where('active = true') .where('age > 18') .limit(10) .build();
// SELECT * FROM users WHERE active = true AND age > 18 LIMIT 10

Exercise: Implement All Three

  1. Build a Logger singleton that writes to console and a file
  2. Create a NotificationFactory that returns Email/SMS/Push senders
  3. Build a RequestBuilder with method/url/headers/body/build()
  4. Write tests that verify the singleton returns the same instance
  5. Use the builder to construct 3 different HTTP requests

Supporting Resources

Go deeper with these references.

Refactoring.Guru
Design Patterns Catalog Visual explanations of all 23 GoF patterns with examples in multiple languages.
GitHub
TypeScript Design Patterns Community collection of design pattern implementations in TypeScript.
O'Reilly
Head First Design Patterns The most approachable book-length treatment of the classic Gang of Four patterns.

Day 1 Checkpoint

Before moving on, make sure you can answer these without looking:

Continue To Day 2
Structural Patterns