Behavioral Design Patterns
How Objects Classes Interact?
This module explores different behavioral design patterns, patterns that describe the way objects and classes interact and divide responsibilities among themselves. A behavioral pattern abstracts an action you want to take from the object or class that takes the action. By changing the object or class, you can change the algorithm used, the objects affected, or the behavior, while still retaining the same basic interface for client classes.
A good toolbox of behavioral patterns allows you to solve many challenging problems you are likely to encounter when designing object-oriented systems. These include enumerating lists, responding to changes of state in an object, serializing and deserializing objects without penetrating data encapsulation.
In this module, you will learn:
- How programmers use behavioral design patterns
- About the most commonly used design patterns
- When to use the Observer and Mediator patterns
- How to use the Mediator pattern to manage the different traffic lights
Behavioral object patterns
use object composition rather than inheritance.
Some of these patterns describe how a group of peer objects cooperate to perform a task that no single object can carry out by itself.
An important issue here is how peer objects know about each other. Peers could maintain explicit references to each other, but that would increase their coupling. In the extreme, every object would know about every other.
pattern avoids this by introducing a mediator object
between peers. The mediator provides the indirection needed for loose coupling.
The Chain of Responsibility
provides even looser coupling
. It lets you send requests to an object implicitly through a chain of candidate objects. Any candidate may fulfill the request depending on run-time conditions. The number of candidates is open-ended
, and you can select which candidates participate in the chain at run-time.
It is true that behavioral patterns are concerned with algorithms and communication between them. Behavioral patterns are a type of software design pattern that identifies common communication patterns between objects. By doing so, these patterns increase flexibility in carrying out communication.
Behavioral patterns are typically used to:
- Decouple senders and receivers of messages
- Encapsulate behavior in objects
- Implement complex control flow
Some examples of behavioral patterns include:
- Chain of responsibility: This pattern allows a request to be passed along a chain of handlers until it is handled by one of them.
- Command: This pattern encapsulates a request as an object, which can then be delayed, queued, or undone.
- Mediator: This pattern provides a unified interface to a set of objects, reducing the complexity of communication between them.
- Observer: This pattern allows objects to be notified of changes to other objects.
- State: This pattern allows an object to alter its behavior when its internal state changes.
- Strategy: This pattern allows different algorithms to be used for the same task, depending on the context.
Behavioral patterns can be used to improve the design of communication between objects in a variety of software systems. For example, the chain of responsibility pattern can be used to implement a workflow system, where different tasks are handled by different objects in the chain. The command pattern can be used to implement a undo/redo system, where requests can be undone or redone later. The mediator pattern can be used to implement a user interface, where different controls communicate with each other through a mediator object.
Overall, behavioral patterns are a powerful tool for designing flexible and robust communication between objects in software systems.
Behavioral design patterns make up most of the design patterns offered by the Gang of Four.
The "Gang of Four" provided eleven behavioral patterns:
- Chain of Responsibility
- Interpreter (class type)
- Template Method (class type)
The key to understanding behavioral design patterns is communication.
The focus shifts from the objects and classes that make up a design pattern to the communication between objects and classes. In the truest form of composition, behavioral patterns are best understood in terms of how objects work together to perform tasks.