Behavioral Patterns  «Prev  Next»

Behavioral Design Patterns in the Context of the Gang of Four Patterns

  1. Introduction to Behavioral Design Patterns:
    1. Definition and primary focus
    2. Importance in object-oriented design
    3. Emphasis on object collaboration and responsibility distribution
  2. Chain of Responsibility:
    1. Definition: Decouples the sender from the receiver by allowing multiple objects to handle a request.
    2. Key Components: Handler, ConcreteHandler, Client
    3. Use Cases: Event handling systems, Command processing
  3. Command:
    1. Definition: Encapsulates a request as an object, allowing parameterization and queuing of requests.
    2. Key Components: Command, ConcreteCommand, Invoker, Receiver
    3. Use Cases: GUI actions, Macro recording, Undo functionality
  4. Interpreter:
    1. Definition: Provides a representation for a language's grammar and an interpreter to deal with this grammar.
    2. Key Components: AbstractExpression, TerminalExpression, NonterminalExpression, Context
    3. Use Cases: Compilers, Parsers, Regular expression engines
  5. Iterator:
    1. Definition: Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.
    2. Key Components: Iterator, ConcreteIterator, Aggregate, ConcreteAggregate
    3. Use Cases: Navigating collections, Unified access to different structures
  6. Mediator:
    1. Definition: Encapsulates how a set of objects interact, reducing direct dependencies between them.
    2. Key Components: Mediator, ConcreteMediator, Colleague, ConcreteColleague
    3. Use Cases: Chat rooms, Air traffic control systems
  7. Memento:
    1. Definition: Captures and externalizes an object's internal state without violating encapsulation, allowing restoration to this state later.
    2. Key Components: Originator, Memento, Caretaker
    3. Use Cases: Undo mechanisms, State checkpoints
  8. Observer:
    1. Definition: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
    2. Key Components: Subject, ConcreteSubject, Observer, ConcreteObserver
    3. Use Cases: Event management systems, Model-View-Controller (MVC) architectures
  9. State:
    1. Definition: Allows an object to change its behavior when its internal state changes.
    2. Key Components: Context, State, ConcreteState
    3. Use Cases: Finite state machines, UI components with multiple states
  10. Strategy:
    1. Definition: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
    2. Key Components: Strategy, ConcreteStrategy, Context
    3. Use Cases: Sorting algorithms, Payment methods
  11. Template Method:
    1. Definition: Defines the skeleton of an algorithm in an algorithm class but defers some steps to subclasses.
    2. Key Components: AbstractClass, ConcreteClass
    3. Use Cases: Frameworks, Code generators
  12. Visitor:
    1. Definition: Represents an operation to be performed on elements of an object structure without changing the classes on which it operates.
    2. Key Components: Visitor, ConcreteVisitor, Element, ConcreteElement, ObjectStructure
    3. Use Cases: Document processing, Symbolic manipulation of expressions
The importance of Behavioral Design Patterns Emphasis on enhancing flexibility, maintainability, and clarity in system design Encouragement for continued exploration and application in real-world scenarios.

The Significance of Behavioral Design Patterns

In the intricate tapestry of software design, Behavioral Design Patterns emerge as the unsung heroes, orchestrating the dance of objects and classes in harmonious synchrony. These patterns, deeply rooted in the principles of object-oriented design, play a pivotal role in shaping the interactions and communications between objects. But why are they so crucial? The magic of Behavioral Design Patterns lies in their ability to enhance three core pillars of software design: flexibility, maintainability, and clarity. By focusing on object collaboration and responsibility distribution, these patterns introduce a level of flexibility that's indispensable. They allow systems to evolve and adapt, accommodating new functionalities without disrupting existing features. This adaptability ensures that as business requirements change, the software can gracefully change with them.
Maintainability is another cornerstone. As software projects grow, they often become more complex, making them harder to understand and modify. Behavioral Design Patterns act as a beacon, guiding developers through this complexity. By establishing well-defined interactions and responsibilities, they reduce ambiguity, making the codebase easier to navigate, understand, and modify. This clarity is invaluable, especially for new developers joining a project or when revisiting older projects. But beyond these technical advantages, there's a narrative that's equally compelling. Behavioral Design Patterns encourage a mindset, a way of thinking that prioritizes design elegance and efficiency. They challenge developers to look beyond the immediate coding task and envision the broader system dynamics, fostering a holistic approach to software design.
In the ever-evolving landscape of technology, where change is the only constant, the timeless wisdom of Behavioral Design Patterns remains relevant. They serve as a testament to the ingenuity of past software architects and a guide for the present. For developers, from the novice to the seasoned, there's an open invitation: Dive deep into these patterns, explore their nuances, and harness their power in real-world scenarios. The journey promises not just better code, but also a richer understanding of the art of software design.
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.
Behavioral patterns influence how state and behavior flow through a system. By optimizing how state and behavior are transferred and modified, you can simplify, optimize, and increase the maintainabilty of an application.
Below is a list of common behavioral design patterns.
  1. Chain of responsibility: Command objects are handled or passed on to other objects by logic-containing processing objects
  2. Command: Command objects encapsulate an action and its parameters
  3. Interpreter: Implement a specialized computer language to rapidly solve a specific set of problems
  4. Iterator: Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
  5. Mediator: Provides a unified interface to a set of interfaces in a subsystem
  6. Memento: Provides the ability to restore an object to its previous state (rollback)
  7. Observer: also known as Publish/Subscribe or Event Listener. Objects register to observe an event that may be raised by another object
  8. State: A clean way for an object to partially change its type at runtime
  9. Strategy: Algorithms can be selected on the fly
  10. Template Method: Describes the program skeleton of a program
  11. Visitor: A way to separate an algorithm from an object


  1. Externalize the Stack:Turn a recursive function into an iterative one that uses a stack.
  2. Hierarchical visitor: Provide a way to visit every node in a hierarchical data structure such as a tree.
  3. Null Object: Designed to act as a default value of an object
  4. Protocol Stack: Communications are handled by multiple layers, which form an encapsulation hierarchy.
  5. Scheduled-task: A task is scheduled to be performed at a particular interval or clock time (used in real-time computing)
  6. Single-serving visitor: Optimise the implementation of a visitor that is allocated, used only once, and then deleted
  7. Specification: Recombinable Business logic in a boolean fashion
  8. Weak reference: De-couple an observer from an observable.

Ad Gang of Four Patterns