Behavioral Patterns  «Prev  Next»
Lesson 2What is a behavioral pattern?
ObjectiveDefine Behavioral Patterns

What is a Behavioral Design Pattern?

In the vast realm of software engineering, the digital landscape teems with objects, each like an inhabitant of an intricate metropolis. Just as in any bustling city, interaction is key. Without proper channels of communication and guidelines, chaos would reign. That’s where Behavioral patterns come in, acting as the architects of these urban interactions.
Imagine a grand city called CodeVille.

In this sprawling urban expanse, each object is like a citizen. Some are shopkeepers, some are mail carriers, and others might be librarians. But for the city to truly thrive, these citizens must communicate, collaborate, and coordinate.
  1. The Mediator - The Efficient Traffic System:
    At the heart of CodeVille is a sophisticated traffic system, ensuring everyone can navigate the streets without chaos. This system, in software terms, mirrors the Mediator pattern. It ensures objects don't communicate directly but instead go through a central system, ensuring smooth coordination without congestion.
  2. The Observer – The Local Newspaper:
    Every morning, the residents of CodeVille eagerly await their copy of the ‘CodeVille Chronicle’. As events unfold, the newspaper (or the Observer) keeps everyone informed. Similarly, in software, when an object changes state, all registered observers are notified, ensuring the system remains informed and up to date.
  3. The Strategy – The City’s Varied Restaurants:
    In every corner of CodeVille, from the Italian Pizzeria to the Japanese Sushi bar, residents have myriad choices for dining. This is much like the Strategy pattern, where algorithms can be selected interchangeably, allowing objects to alter their functionalities according to the strategy they employ.
  4. The Command – The City's Mail Service:
    Postmen of CodeVille take their mail delivery very seriously. Each letter they carry is like a command, ensuring the intended action (or message) reaches the recipient. Similarly, in the Command pattern, objects encapsulate actions as commands, decoupling the sender from the receiver.
  5. The Iterator – The City’s Museum Tour:
    CodeVille's renowned museum offers guided tours, letting visitors explore its vast collections in a structured manner. This mirrors the Iterator pattern, providing a way to access the elements of a collection object sequentially without exposing its underlying representation.

Through the avenues of CodeVille, amidst its digital skyscrapers and binary trees, Behavioral patterns work silently yet persistently. They establish channels, create pathways, and devise rules, ensuring the city remains a hub of harmony, innovation, and collaboration. By dictating how objects interact, they keep the heartbeat of CodeVille rhythmic and robust, allowing it to flourish as a masterpiece of software engineering.

How Objects interact

A behavioral pattern explains how objects interact. In addition, this pattern describes how different objects and classes send messages to each other to make things happen and how the steps of a task are divided among different objects. Where
  1. creational patterns mostly describe a moment of time (the instant of creation), and
  2. structural patterns describe a more or less static structure,
  3. behavioral patterns describe a process or a flow.

Behavioral Class Patterns

Behavioral class patterns use inheritance, subclassing, and polymorphism to adjust the steps taken during a process. Furthermore, behavioral class patterns focus on changing the exact algorithm used or task performed depending on circumstances.

Behavioral Object Patterns

Behavioral object patterns describe how different objects work together to accomplish a task. Behavioral object patterns accomplish tasks that would be difficult or impossible to accomplish with single objects. Furthermore, they generally make the entire flow simpler, more understandable, and more robust than the string-and-bailing-wire solutions that are built without a clear design in mind.

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.
Examples of this type of design pattern include:
  1. Chain of responsibility Pattern:: Command objects are handled or passed on to other objects by logic-containing processing objects
  2. Command Pattern:: Command objects encapsulate an action and its parameters
  3. Externalize the Stack: Turn a recursive function into an iterative one that uses a stack.
  4. Hierarchical Visitor Pattern:: Provide a way to visit every node in a hierarchical data structure such as a tree.
  5. Interpreter Pattern:: Implement a specialized computer language to rapidly solve a specific set of problems
  6. Iterator Pattern:: Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
  7. Mediator Pattern:: Provides a unified interface to a set of interfaces in a subsystem
  8. Memento Pattern:: Provides the ability to restore an object to its previous state (rollback)
  9. Null Object Pattern:: designed to act as a default value of an object
  10. Observer Pattern:: aka Publish/Subscribe or Event Listener. Objects register to observe an event that may be raised by another object
  11. Weak reference Pattern:: De-couple an observer from an observable.
  12. Protocol stack: Communications are handled by multiple layers, which form an encapsulation hierarchy.
  13. Scheduled-task Pattern:: A task is scheduled to be performed at a particular interval or clock time (used in real-time computing)
  14. Single-serving visitor Pattern:: Optimise the implementation of a visitor that is allocated, used only once, and then deleted
  15. Specification Pattern:: Recombinable Business logic in a boolean fashion
  16. State Pattern:: A clean way for an object to partially change its type at runtime
  17. Strategy Pattern:: Algorithms can be selected on the fly
  18. Template method Pattern:: Describes the program skeleton of a program
  19. Visitor Pattern:: A way to separate an algorithm from an object