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
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.
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.
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.