Behavioral Patterns  «Prev  Next»
Lesson 1

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: 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:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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:
  1. How programmers use behavioral design patterns
  2. About the most commonly used design patterns
  3. When to use the Observer and Mediator patterns
  4. 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

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

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: This pattern creates a chain of handler objects to process requests. Each handler decides whether to process the request itself or pass it along to the next handler in the chain.
  • Command: This pattern encapsulates a request as an object, letting you parameterize clients with different requests, queue or log requests, and support undoable operations. It decouples the object that invokes the operation from the one that knows how to perform it.
  • Interpreter: This pattern defines a grammatical representation for a language and provides an interpreter to evaluate sentences in the language. It's often used for parsing and interpreting specialized languages.
  • Iterator: This pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It abstracts the traversal of a collection.
  • Mediator: This pattern defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly.
  • Memento: This pattern captures and externalizes an object's internal state so that the object can be restored to this state later without violating encapsulation. It allows for implementing undo mechanisms.
  • Observer: This pattern defines a one-to-many dependency between objects so that when one object's state changes, all its dependents are notified and updated automatically. It's commonly used for event handling.
  • State: This pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
  • Strategy: This pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from the clients that use it.
  • Template Method: This pattern defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. It provides a template for creating similar algorithms.
  • Visitor: This pattern represents an operation to be performed on the elements of an object structure. Visitor lets you define new operations without changing the classes of the elements on which it operates.

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.
Behavioral patterns focus on communication between pattern participants
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.

SEMrush Software 1 SEMrush Banner 1