Behavioral design patterns, the maestros of object interaction, weave a grand tapestry of communication among classes and objects. Just as a conductor ensures that every instrument in an orchestra plays in harmony, these patterns orchestrate how components in a system should converse, ensuring that the software's symphony sings without a discordant note.
Let's dive deeper into the poetic nuances of Behavioral design patterns:
Behavioral design patterns, the maestros of object interaction, weave a grand tapestry of communication among classes and objects. Just as a conductor ensures that every instrument in an orchestra plays in harmony, these patterns orchestrate how components in a system should converse, ensuring that the software's symphony sings without a discordant note.
Let's dive deeper into the poetic nuances of Behavioral design patterns:
Dancers in a Ballet: Behavioral patterns dictate the choreography of objects in a system, much like how a dance troupe follows its choreographer. Each object knows its step, its turn, and when to take the stage, ensuring the dance is fluid and cohesive.
The Whispering Winds: They embody the principle of loose coupling. Just as winds carry messages from one place to another without binding to the source or destination, objects in these patterns interact without being tightly bound to each other, promoting flexibility and scalability.
Navigators of Change: Like seasoned sailors navigating rough seas, Behavioral patterns allow for easy adaptability. If one object changes, others can easily adapt without needing a complete overhaul.
The Storytellers: Every story has characters that interact in unique ways, bringing the tale to life. Behavioral patterns give objects distinct roles, ensuring each plays its part in the narrative of the software.
Guardians of Responsibilities: Just as knights of old had defined roles in a kingdom's defense, objects in Behavioral patterns have clear responsibilities. They ensure no single object is burdened with too many tasks, promoting delegation and a clear division of duty.
The Intermediaries: Sometimes, direct communication can be chaotic. Imagine a bustling market with everyone talking at once. Behavioral patterns often introduce intermediaries, much like translators in a busy marketplace, ensuring messages are conveyed effectively without overwhelming direct channels.
Dynamic and Reactive: Like a responsive echo in a vast canyon, these patterns often involve reactions to events or actions. They don't just lay down static rules; they react and adapt, making systems dynamic and interactive.
In essence, Behavioral design patterns are the soulful poets of the software design world. They don't just construct rigid bridges between objects; they craft intricate dances, mesmerizing stories, and responsive dialogues. They ensure that software is not just functional but also artfully orchestrated, allowing components to communicate in a ballet of code and design.
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.
The Mediator 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.
Behavioral patterns are concerned with algorithms and communication between them. The operations that make up a single algorithm might be split up between different classes, making a complex arrangement that is difficult to manage and maintain. The behavioral patterns capture ways of expressing the division of operations between classes and optimize how the communication should be handled. In this first chapter on behavioral patterns, we will look at three simple but very useful patterns: the Strategy, State, and Template Methods.
Behavioral design patterns make up the plurality of design patterns offered by the Gang of Four. This section covers one class example (Template Method) and one object example (State). All in all, Gamma, Helm, Johnson, and Vlissides provide eleven behavioral patterns:
Chain of Responsibility
Command
Interpreter (class type)
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method (class type)
Visitor
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.
Figure 6-1: Behavioral patterns focus on communication between pattern participants
The emphasis on interaction between the elements that make up a pattern is so important that some class diagrams look identical, such as the State and Strategy patterns. However, because of the way in which the participants communicate and how they handle responsibilities, they are very different.