A comprehensive collection of design patterns implemented in TypeScript with real-world examples, unit tests, and interview preparation materials.
This repository is designed to help you:
- Master design patterns with practical TypeScript implementations
- Prepare for interviews with common questions and answers
- Understand when to use each pattern with real-world scenarios
- Practice coding with fully tested, production-ready examples
These patterns are intuitive, commonly used, and frequently asked in interviews:
- Strategy Pattern - Encapsulate algorithms and make them interchangeable
- Factory Pattern - Create objects without specifying exact classes
- Observer Pattern - Subscribe to and receive notifications of events
- Decorator Pattern - Add functionality to objects dynamically
- Singleton Pattern - Ensure only one instance exists
- Adapter Pattern - Make incompatible interfaces work together
- Facade Pattern - Provide simplified interface to complex systems
- Proxy Pattern - Control access to objects
- Composite Pattern - Treat individual and composite objects uniformly
- Command Pattern - Encapsulate requests as objects
- Template Method - Define algorithm skeleton, let subclasses override steps
- State Pattern - Change behavior based on internal state
- Chain of Responsibility - Pass requests along a chain of handlers
- Builder Pattern - Construct complex objects step by step
- Abstract Factory - Create families of related objects
- Prototype Pattern - Clone existing objects
- Node.js (v18 or higher)
- npm or yarn
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Generate coverage report
npm run test:coverage
# Build TypeScript
npm run build- Start with easier patterns (Strategy, Factory, Observer)
- Read each pattern's README to understand:
- What problem it solves
- When to use it
- Pros and cons
- Study the implementation code
- Run and modify the tests
- Try implementing your own variations
- Review the INTERVIEW_GUIDE.md
- Practice explaining each pattern in your own words
- Memorize the UML diagrams and class structures
- Be ready to code patterns from scratch
- Understand trade-offs and alternatives
- Location:
src/behavioral/strategy/ - Tests: 25 test cases covering all scenarios
- Real-world example: E-commerce payment processing (Credit Card, PayPal, Crypto)
- Key concept: Encapsulate algorithms to make them interchangeable
- Interview focus: Decision patterns, polymorphism, context switching
- Read: Strategy Pattern Guide
- Location:
src/creational/factory/ - Tests: 50 test cases with multiple factory examples
- Real-world examples:
- Document Factory (PDF, Word, Excel)
- Shape Factory (Circle, Rectangle, Triangle)
- Notification Factory (Email, SMS, Push, Slack) with registry pattern
- Key concept: Create objects without specifying their exact classes
- Interview focus: Encapsulation, loose coupling, extensibility
- Read: Factory Pattern Guide
- Location:
src/behavioral/observer/ - Tests: 40 test cases including integration tests
- Real-world examples:
- Stock price notification system (Traders, Portfolio Tracker, Alert Service, Logger)
- User activity tracking system (Analytics, Notifications, Security monitoring)
- Key concept: Define one-to-many dependency with automatic notifications
- Interview focus: Event-driven architecture, loose coupling, pub-sub pattern
- Read: Observer Pattern Guide
- Location:
src/structural/decorator/ - Tests: 36 test cases with decorator combinations
- Real-world examples:
- Coffee shop system (Milk, Sugar, Whipped Cream, Caramel, Vanilla decorators)
- Text formatting system (Bold, Italic, Underline, Color, Highlight decorators)
- Data stream system (Compression, Encryption, Validation, Buffering decorators)
- Key concept: Add behavior to objects dynamically without subclassing
- Interview focus: Composition over inheritance, decorator stacking, behavior modification
- Read: Decorator Pattern Guide
- Location:
src/creational/singleton/ - Tests: 46 test cases covering initialization and state management
- Real-world examples:
- Logger singleton for application-wide logging
- Database connection pool singleton
- Configuration manager singleton
- Cache manager singleton
- Key concept: Ensure a class has only one instance with global access
- Interview focus: Lazy initialization, thread safety, testability concerns, DI alternative
- Read: Singleton Pattern Guide
src/
βββ behavioral/ # Behavioral patterns
β βββ strategy/
β β βββ strategy.ts
β β βββ strategy.test.ts
β β βββ README.md
β βββ ...
βββ creational/ # Creational patterns
β βββ factory/
β βββ ...
βββ structural/ # Structural patterns
β βββ decorator/
β βββ ...
βββ examples/ # Real-world examples
βββ use-cases/
| Pattern | Category | Difficulty | Tests | Location | Use When |
|---|---|---|---|---|---|
| Strategy | Behavioral | β Easy | 25 | src/behavioral/strategy/ |
Multiple algorithms for same task |
| Factory | Creational | β Easy | 50 | src/creational/factory/ |
Object creation logic is complex |
| Observer | Behavioral | β Easy | 40 | src/behavioral/observer/ |
One-to-many event notifications |
| Decorator | Structural | ββ Medium | 36 | src/structural/decorator/ |
Add features without subclassing |
| Singleton | Creational | β Easy | 46 | src/creational/singleton/ |
Only one instance needed |
| Pattern | Category | Difficulty | Use When |
|---|---|---|---|
| Adapter | Structural | ββ Medium | Incompatible interfaces |
| Facade | Structural | β Easy | Simplify complex subsystems |
| Command | Behavioral | ββ Medium | Parameterize and queue operations |
| Builder | Creational | ββ Medium | Complex object construction |
| Proxy | Structural | ββ Medium | Control object access |
- Singleton - Almost always asked β Implemented
- Factory - Very common β Implemented
- Observer - Common for frontend roles β Implemented
- Strategy - Common for algorithm-heavy roles β Implemented
- Decorator - Common for all roles β Implemented
Strategy vs State
- Strategy: Client chooses algorithm; behavior varies by external decision
- State: Object changes behavior based on internal state; object controls transition
Factory vs Singleton
- Factory: Creates instances (potentially multiple)
- Singleton: Ensures only ONE instance exists
Decorator vs Strategy
- Decorator: Adds behavior to objects at runtime; composition-based
- Strategy: Encapsulates algorithm selection; composition-based
Observer vs Mediator
- Observer: One-to-many; subjects notify observers directly
- Mediator: Many-to-many; objects communicate through central mediator
- β Clear explanation of the problem the pattern solves
- β Knowledge of when NOT to use the pattern
- β Ability to implement from scratch without notes
- β Understanding of trade-offs and alternatives
- β Real-world examples from your experience
- β How to test code that uses the pattern
- β Performance implications
- β Memorizing code without understanding
- β Not knowing the problems patterns solve
- β Overusing patterns where simple code would work
- β Confusing similar patterns (e.g., Strategy vs State)
- β Not being able to explain why you'd use a pattern
- β Claiming all patterns are equally useful everywhere
Test Suites: 5 passed, 5 total
Tests: 193 passed, 193 total β
Pattern Breakdown:
- Strategy: 25 tests β
- Factory: 50 tests β
- Observer: 40 tests β
- Decorator: 36 tests β
- Singleton: 46 tests β
Run tests locally:
npm test # Run all tests
npm test -- --watch # Watch mode
npm run coverage # Coverage report- Refactoring.Guru - Design Patterns - Excellent visual explanations
- TypeScript Handbook - Official TypeScript docs
- Gang of Four Book - Original design patterns (1994)
- SOLID Principles - Design principles that patterns follow
- β Already completed: Strategy, Factory, Observer, Decorator, Singleton
- π Start here: Review CHEAT_SHEET.md for quick reference
- π Study each pattern: Start with Strategy Pattern
- π» Practice: Modify tests and implement your own variations
- π€ Interview prep: Use real-world examples to explain each pattern
Feel free to:
- Add more examples to existing patterns
- Improve documentation or fix bugs
- Share this with others preparing for interviews
- Suggest new patterns to implement
This is a learning resource - improvements welcome!
MIT - Use this freely for learning and interview preparation.
Ready to start? Begin with the Strategy Pattern guide π
Need a quick review? Check out the CHEAT_SHEET.md for one-liners and decision matrices.