What are Behavioral Patterns?
Behavioral patterns are concerned with assignment of responsibilities between objects. What makes them different from structural patterns is they don’t just specify the structure but also outline the patterns for message passing/communication between them.
Definition
According to Wikipedia:In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
Available Patterns
This section covers the following behavioral design patterns:Chain of Responsibility
Build a chain of objects to handle requests
Command
Encapsulate actions as objects
Iterator
Access elements sequentially without exposing structure
Mediator
Control interaction between objects through a mediator
Memento
Capture and restore object state
Observer
Notify dependents when object state changes
Visitor
Add operations to objects without modifying them
Strategy
Switch algorithms based on the situation
State
Change behavior when state changes
Template Method
Define algorithm skeleton, defer steps to subclasses
When to Use Behavioral Patterns
Consider using behavioral patterns when:- You need to define communication between multiple objects
- You want to encapsulate behavior or algorithms
- You need to implement undo/redo functionality
- You want to decouple senders and receivers of requests
- You need to traverse collections in different ways
- You want to add operations without modifying existing code