Design Patterns
OOP design patterns with interactive diagrams and code examples in TypeScript, Python, Java, and Go.
Design patterns are reusable solutions to commonly occurring problems in software design. Each pattern page includes a class diagram, implementation examples in all four languages, and a discussion of trade-offs.
Creational Patterns
These patterns deal with object creation mechanisms, aiming to create objects in a manner suitable to the situation.
| Pattern | Description |
|---|---|
| Singleton | Ensure a class has only one instance and provide a global access point |
| Factory Method | Define an interface for creating objects, letting subclasses decide which class to instantiate |
| Abstract Factory | Produce families of related objects without specifying their concrete classes |
| Builder | Construct complex objects step by step, separating construction from representation |
| Prototype | Clone existing objects without coupling to their classes |
Structural Patterns
These patterns explain how to assemble objects and classes into larger structures while keeping them flexible and efficient.
| Pattern | Description |
|---|---|
| Adapter | Convert the interface of a class into another interface clients expect |
| Bridge | Decouple an abstraction from its implementation so the two can vary independently |
| Composite | Compose objects into tree structures to represent part-whole hierarchies |
| Decorator | Attach additional responsibilities to an object dynamically |
| Facade | Provide a simplified interface to a complex subsystem |
| Flyweight | Use sharing to support large numbers of fine-grained objects efficiently |
| Proxy | Provide a surrogate or placeholder for another object to control access |
Behavioral Patterns
These patterns are concerned with algorithms and the assignment of responsibilities between objects.
| Pattern | Description |
|---|---|
| Chain of Responsibility | Pass a request along a chain of handlers until one handles it |
| Command | Encapsulate a request as an object, enabling undo, queuing, and logging |
| Iterator | Access elements of a collection sequentially without exposing its representation |
| Mediator | Define an object that encapsulates how a set of objects interact |
| Memento | Capture and restore an object's internal state without violating encapsulation |
| Observer | Define a one-to-many dependency so dependents are notified of state changes |
| State | Allow an object to alter its behavior when its internal state changes |
| Strategy | Define a family of algorithms, encapsulate each one, and make them interchangeable |
| Template Method | Define the skeleton of an algorithm, letting subclasses override specific steps |
| Visitor | Define new operations on elements without changing their classes |