Behavioral Patterns: Characteristics and Overview in the Context of the Gang of Four Patterns
In the realm of software design, the Gang of Four (GoF) patterns stand as a seminal work, offering 23 design patterns categorized into three groups: Creational, Structural, and Behavioral. Among these, Behavioral Patterns focus on the communication between objects, ensuring efficient and flexible interactions.
Fundamental Characteristics of Behavioral Patterns:
Object Interactions: Behavioral Patterns primarily address the responsibilities of objects and how they communicate. They ensure that objects collaborate effectively, distributing responsibilities among them.
Decoupling: One of the primary goals of Behavioral Patterns is to decouple classes, ensuring that interacting objects remain loosely coupled. This decoupling fosters flexibility in the system, allowing for easier modification and extension.
Responsibility Chains: These patterns often define how requests are passed between a chain of objects, ensuring that the request is handled appropriately without explicitly specifying the receiver.
Centralized Control: Some Behavioral Patterns centralize control in a single object, which manages the logic and coordination for the entire system.
Flexibility in Object Interactions: Behavioral Patterns provide solutions that allow for dynamic changes in the interactions between objects, ensuring adaptability in the face of changing requirements.
Notable Behavioral Patterns
To provide a clearer understanding, here are some of the key Behavioral Patterns as defined by the Gang of Four:
Observer: Defines a dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Mediator: Encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly.
Command: Turns a request into a stand-alone object that contains information about the request, allowing for parameterization of clients with different requests, queuing of requests, and logging of operations.
Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing the algorithm to vary independently from clients that use it.
State: Allows an object to alter its behavior when its internal state changes, making it appear as though the object's class has changed.
Template Method: Defines the skeleton of an algorithm in a method in an algorithm class but delays some steps to subclasses, allowing subclasses to redefine certain steps of an algorithm without changing the algorithm's structure.
Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Chain of Responsibility: Decouples the sender from the receiver by allowing more than one object to handle a request. The request is passed along a chain until an object handles it or it reaches the end of the chain.
Visitor: Represents an operation to be performed on elements of an object structure, allowing for new operations to be added without modifying the classes of the elements on which they operate.
Memento: Captures and externalizes an object's internal state without violating encapsulation, allowing the object to be restored to this state later.
Behavioral Patterns, as delineated by the Gang of Four, offer a structured approach to handle interactions and communications between objects. By focusing on the distribution of responsibilities and ensuring that objects remain loosely coupled, these patterns provide solutions that enhance flexibility, adaptability, and maintainability in software design.
Behavioral patterns describe interactions between objects. They focus on how objects communicate with each other.
Behavioral object patterns use object composition rather than inheritance. Some 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.
Make objects of the right class for a problem, generally when instances of several different classes are available.
There are two recurring themes in creational patterns. First, they all encapsulate knowledge about which concrete classes the system uses.
Second, they hide how instances of these classes are created and put together.
All the system at large knows about the objects is their interfaces as defined by abstract classes. Consequently, the creational patterns give you a lot of flexibility in what gets created, who creates it, how it gets created, and when.
They let you configure a system with "product" objects that vary widely in structure and functionality. Configuration can be static (that is, specified at compile-time) or dynamic (at run-time).
Creational design patterns are those that focus on the instantiation process. These patterns
are designed to conceal the creation process from the instances created and encapsulate
knowledge used by the objects. The five creational design patterns include:
Form larger structures from individual parts, generally of different classes. Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new functionality. The added flexibility of object composition comes from the ability to change the composition at run-time, which is impossible with static class composition.
Structural design patterns examine how objects and classes are composed to form larger structures. In class structural design, new structures are created through multiple inheritance. One class inherits from more than a single parent class to create a new structure. More commonly, object structures combine different objects to form new structures. The following seven patterns have been designated as structural.