There are many popular
behavioral design patterns which you will encounter while writing code or developing software.
There are many relationships between the common behavioral patterns. Chain of Responsibility, Command Pattern, Iterator Pattern, Mediator, Observer Pattern, Strategy Pattern and Visitor Pattern. They are all concerned with how objects communicate and interact with each other.
Chain of Responsibility and
Command are both concerned with decoupling senders and receivers of requests.
In Chain of Responsibility, a request is passed along a chain of potential handlers until one of them handles it. In Command, a request is encapsulated in an object that can be passed around and executed later.
Mediator and
Observer are both concerned with loosely coupling objects that need to communicate with each other. In Mediator, all communication between objects goes through a central mediator object. In Observer, objects can subscribe to and unsubscribe from receiving notifications from other objects.
Strategy and
Visitor are both concerned with encapsulating behavior in objects and making it interchangeable. In Strategy, different algorithms are implemented in different strategy objects, which can be plugged into a context object to change its behavior. In Visitor, different operations are implemented in different visitor objects, which can be used to traverse and operate on object structures.
Here are some specific examples of relationships between the patterns:
- Chain of Responsibility and Mediator can be used together to create a dynamic chain of handlers that is mediated by a central mediator object. This can be useful for implementing complex routing or authorization logic.
- Command and Observer can be used together to implement undo/redo functionality. When a command is executed, it can notify observers of the change. If the command is undone, the observers can be notified again to restore the previous state.
- Strategy and Visitor can be used together to implement generic algorithms. For example, a generic sorting algorithm could use a visitor to compare and swap elements of a collection.
Overall, the common behavioral patterns are a powerful set of tools that can be used to design flexible and extensible software. By understanding the relationships between the patterns, you can choose the right pattern for the job and combine them in creative ways.
The following
series of images briefly describes seven common behavioral patterns:
In the context of the Gang of Four (GoF), behavioral patterns are design patterns that focus primarily on the communication and interaction between objects and classes to achieve greater flexibility and organization in software systems. These patterns help in defining how objects are created, composed, and interact, thus allowing for the effective distribution of responsibilities among objects.
Behavioral patterns such as the Observer pattern illustrate interaction by creating a one-to-many dependency between objects where changes in one object lead to automatic updates in others. This pattern is fundamental for implementing distributed event handling systems where objects need to be notified of changes in other parts of the system. Another example is the Strategy pattern, which encapsulates algorithms inside classes, allowing the behavior of an object to change dynamically at runtime by selecting different strategies. This promotes loose coupling by allowing the client code to work with an interface or abstract class rather than concrete implementations, facilitating easier switching between different behaviors without altering the client code.
The Command pattern encapsulates a request as an object, thus allowing for parameterization of clients with different requests, queue or log requests, and support undoable operations. This pattern decouples the object that invokes the operation from the one that knows how to perform it, which is particularly useful in UI frameworks where actions like clicks or key presses can be treated uniformly as commands. Additionally, the Chain of Responsibility pattern passes a request along a chain of handlers, with each handler deciding either to process the request or pass it to the next handler in the chain. This pattern is beneficial for scenarios where multiple objects might handle a request, and the exact handler is determined at runtime, such as in logging or event processing systems. Through these patterns, the GoF provides models for how objects can interact in a way that promotes reusability, extensibility, and robustness in software design.