Design Patterns and Real Life Analogies
1. Creational Patterns
- Singleton: Only one instance of a class can exist. Like the president of a country, there's only one at a time.
- Factory Method: A creator class decides which product class to instantiate, like a hiring manager deciding which type of employee to hire based on the job description.
- Abstract Factory: A factory of factories. A furniture store that can sell both modern and antique furniture sets.
- Builder: Breaks the construction of a complex object into multiple steps. Like ordering a custom pizza, where you specify the base, toppings, sauce, etc.
- Prototype: Creates new objects by cloning an existing object. Like photocopying a document.
2. Structural Patterns
- Adapter: Allows classes with incompatible interfaces to work together. Like using a travel adapter to charge your phone abroad.
- Bridge: Separates an object's abstraction from its implementation, so they can vary independently. Like choosing a book - you decide on the language (implementation) and genre (abstraction) independently.
- Composite: Allows treating a group of objects the same way as a single instance of an object. Like treating a bundle of stationery items as a single item.
- Decorator: Dynamically adds/overrides behavior in an object. Like adding accessories to your smartphone (e.g., case, pop-socket).
- Facade: Provides a simplified interface to a complex subsystem. Like a remote control for your home theater system.
- Flyweight: Reduces the cost of creating and manipulating a large number of similar objects. Like a public bike-sharing system where users share bikes rather than owning them.
- Proxy: An interface to other objects. Like a credit card is a proxy for a bank account.
3. Behavioral Patterns
- Chain of Responsibility: Allows an object to pass the request along the chain of potential handlers until it's handled. Like in customer support, if an agent can't solve your problem, they pass you on to the next level.
- Command: Encapsulates a request as an object, allowing the parameterization of clients. Like a remote control where each button (command) is bound to a specific action in a device.
- Interpreter: Defines the grammar for instructions that are part of a language or an application. Like Google Translate, interpreting one language into another.
- Iterator: Provides a way to access the elements of an aggregate object without exposing its underlying representation. Like browsing through a playlist on Spotify.
- Mediator: Encapsulates how a set of objects interact. Like an air traffic control tower coordinating airplanes.
- Memento: Captures the current state of an object and store it in such a manner that it can be restored at a later time. Like a save game feature in a video game.
- Observer: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified. Like subscribers getting notified when a YouTuber they follow posts a new video.
- State: Allows an object to alter its behavior when its internal state changes. Like a traffic light changing its behavior based on its current state (red, green, or yellow).
- Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Like using different navigation apps (Google Maps, Waze) to find the best route.
- Template Method: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Like a cooking recipe, with some parts left for variation (e.g., "season to taste").
- Visitor: Separates an algorithm from an object structure on which it operates. Like a tourist visiting different cities, performing a set of actions in each, depending on the type of city.