Behavioral Patterns
13. Chain of Responsibility
Definition: Passes a request along a chain of handlers, allowing each handler to either
process the request or pass it to the next handler in the chain.
When to Use: When you have multiple objects that can handle a request, but you want to
leave it to the objects themselves to decide which one will handle it.
Real-World Examples:
Customer Support: Escalating a customer’s issue through different levels of support
until it is resolved.
Event Handling: Handling UI events like clicks or key presses in a sequence of
handlers.
14. Command
Definition: Encapsulates a request as an object, allowing for parameterization of clients with
queues, requests, and operations.
When to Use: When you want to decouple the sender of a request from the object that
performs the action.
Real-World Examples:
Remote Control: Storing button presses as commands to be executed later.
Task Scheduling: Queuing up tasks to be executed by a task scheduler.
15. Interpreter
Definition: Defines a representation for a language’s grammar and provides an interpreter to
deal with this grammar.
When to Use: When you need to interpret a language or execute scripts.
Real-World Examples:
Regular Expressions: Interpreting and executing pattern-matching rules.
Mathematical Expression Evaluator: Parsing and evaluating math expressions.
16. Iterator
Definition: Provides a way to access the elements of an aggregate object sequentially
without exposing its underlying representation.
When to Use: When you need to traverse a collection of objects without exposing its
internal structure.
Real-World Examples:
List Iteration: Traversing through a list of items like an array or a collection.
File Directory Traversal: Iterating over files in a directory structure.
17. Mediator
Definition: Defines an object that encapsulates how a set of objects interact, promoting
loose coupling by keeping objects from referring to each other explicitly.
When to Use: When you want to reduce the complexity of communication between
multiple objects.
Real-World Examples:
Air Traffic Control: Acting as a mediator between airplanes to prevent collisions.
Chat Room: A chat application where the server mediates the communication
between users.
18. Memento
Definition: Captures and externalizes an object’s internal state so that it can be restored to
this state later without violating encapsulation.
When to Use: When you need to save and restore an object’s state.
Real-World Examples:
Undo Functionality: Implementing undo operations in text editors or games.
Game Save State: Saving and loading the state of a game to continue later.
19. Observer
Definition: Defines a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically.
When to Use: When multiple objects need to be notified and updated automatically when
another object changes.
Real-World Examples:
News Subscription: Notifying subscribers when a new article is published.
Event Listeners: Updating UI components when data changes in an application.
20. State
Definition: Allows an object to alter its behavior when its internal state changes, appearing
to change its class.
When to Use: When an object’s behavior depends on its state and it must change its
behavior at runtime.
Real-World Examples:
Traffic Light: Changing behavior (color) based on its state (red
20. State
Definition: Allows an object to alter its behavior when its internal state changes, appearing
to change its class.
When to Use: When an object’s behavior depends on its state and it must change its
behavior at runtime.
Real-World Examples:
Traffic Light: A traffic light changes behavior (color) based on its state (red, yellow,
green).
Media Player: A media player changes its behavior based on whether it’s in playing,
paused, or stopped state.
21. Strategy
Definition: Defines a family of algorithms, encapsulates each one, and makes them
interchangeable. The strategy pattern allows the algorithm to vary independently from
clients that use it.
When to Use: When you have multiple algorithms for a specific task and you want to swap
them easily.
Real-World Examples:
Sorting Algorithms: Different sorting strategies (e.g., quick sort, merge sort) that can
be selected at runtime.
Payment Methods: An e-commerce platform where users can choose between
different payment strategies (credit card, PayPal, etc.).
22. Template Method
Definition: Defines the skeleton of an algorithm in a method, deferring some steps to
subclasses. This allows subclasses to redefine certain steps of the algorithm without
changing its structure.
When to Use: When you have an algorithm that needs to be extended by subclasses, but
you want to ensure that the structure of the algorithm remains the same.
Real-World Examples:
Cooking Recipes: A general recipe template where specific steps (e.g., add spices,
cook meat) are defined by subclasses.
Document Processing: A document processing system where the general structure
(e.g., open, process, close) is fixed, but the processing step can vary.
23. Visitor
Definition: Represents an operation to be performed on the elements of an object structure.
The visitor pattern allows you to add further operations to this structure without modifying
its elements.
When to Use: When you need to perform operations on elements of a complex object
structure, and you want to keep these operations separate from the object structure itself.
Real-World Examples:
Tax Calculation: Different tax calculations for different product types in an inventory
system.
Syntax Tree Evaluation: Applying operations like type-checking or code generation to
different nodes in a syntax tree.
Conclusion
Understanding and applying design patterns is essential for creating efficient, maintainable,
and scalable software. This cheat sheet provides a quick reference to the Gang of Four
design patterns, helping you to choose the appropriate pattern for your design problems.
Keep this guide handy to make the most of design patterns in your software development.