Design patterns are proven solutions to recurring software design problems. They provide templates for solving common challenges in a maintainable and elegant way. This guide links to detailed explanations and real-world examples for all 23 design patterns organized by their purpose.
These patterns focus on object creation mechanisms, abstracting the instantiation process.
Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Real-world example: Cross-platform UI components
Separates the construction of a complex object from its representation.
Real-world example: Document generator
Defines an interface for creating an object, but lets subclasses decide which class to instantiate.
Real-world example: Payment processing system
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Real-world example: Document template system
Ensures a class has only one instance and provides a global point of access to it.
Real-world example: Database connection manager
These patterns deal with object composition, creating relationships between objects.
Converts the interface of a class into another interface clients expect.
Real-world example: Legacy payment system integration
Decouples an abstraction from its implementation so that the two can vary independently.
Real-world example: Device remote control system
Composes objects into tree structures to represent part-whole hierarchies. Lets clients treat individual objects and compositions uniformly.
Real-world example: File system structure
Attaches additional responsibilities to an object dynamically.
Real-world example: Coffee ordering system
Provides a unified interface to a set of interfaces in a subsystem. Defines a higher-level interface that makes the subsystem easier to use.
Real-world example: Home theater system
Uses sharing to support large numbers of similar objects efficiently.
Real-world example: Text editor character rendering
Provides a surrogate or placeholder for another object to control access to it.
Real-world example: Image loading system
These patterns focus on communication between objects.
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Real-world example: Support ticket handling system
Encapsulate a request as an object, thereby allowing for parameterization of clients with different requests, queuing of requests, and logging of the requests.
Real-world example: Smart home automation
Define a representation for a grammar of a given language along with an interpreter that uses the representation to interpret sentences in the language.
Real-world example: Simple query language interpreter
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Real-world example: Library book collection traversal
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly.
Real-world example: Air traffic control system
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
Real-world example: Text editor undo/redo functionality
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically.
Real-world example: Stock market monitoring system
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Real-world example: Package delivery system
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Real-world example: Payment processing strategy
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Real-world example: Data mining application
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Real-world example: Document object model operations
- Creational: When you need flexibility in creating objects
- Structural: When you need to compose objects into larger structures
- Behavioral: When you need to define how objects communicate
- Don't overuse patterns - only apply them where they add value
- Understand the intent behind each pattern
- Start simple - refactor to patterns when needed
- Combine patterns for complex solutions
Design patterns should make your code more maintainable and flexible, not more complex. Always evaluate if a pattern truly solves your specific problem before implementing it.