Behavioral Patterns  «Prev  Next»
Lesson 4Common behavioral patterns
ObjectiveDescribe the Common Behavioral Patterns

Describe Common Behavioral Patterns known

There are many popular behavioral design patterns which you will encounter while writing code or developing software.

There are many relationships between the common behavioral patterns. Chain of Responsibility, Command Pattern, Iterator Pattern, Mediator, Observer Pattern, Strategy Pattern and Visitor Pattern. They are all concerned with how objects communicate and interact with each other.
Chain of Responsibility and Command are both concerned with decoupling senders and receivers of requests. In Chain of Responsibility, a request is passed along a chain of potential handlers until one of them handles it. In Command, a request is encapsulated in an object that can be passed around and executed later.
Mediator and Observer are both concerned with loosely coupling objects that need to communicate with each other. In Mediator, all communication between objects goes through a central mediator object. In Observer, objects can subscribe to and unsubscribe from receiving notifications from other objects.
Strategy and Visitor are both concerned with encapsulating behavior in objects and making it interchangeable. In Strategy, different algorithms are implemented in different strategy objects, which can be plugged into a context object to change its behavior. In Visitor, different operations are implemented in different visitor objects, which can be used to traverse and operate on object structures.
Here are some specific examples of relationships between the patterns:
  • Chain of Responsibility and Mediator can be used together to create a dynamic chain of handlers that is mediated by a central mediator object. This can be useful for implementing complex routing or authorization logic.
  • Command and Observer can be used together to implement undo/redo functionality. When a command is executed, it can notify observers of the change. If the command is undone, the observers can be notified again to restore the previous state.
  • Strategy and Visitor can be used together to implement generic algorithms. For example, a generic sorting algorithm could use a visitor to compare and swap elements of a collection.

Overall, the common behavioral patterns are a powerful set of tools that can be used to design flexible and extensible software. By understanding the relationships between the patterns, you can choose the right pattern for the job and combine them in creative ways. The following series of images briefly describes seven common behavioral patterns:

Common "behavioral patterns" describe the way Objects and Classes interact

In the context of the Gang of Four (GoF), behavioral patterns are design patterns that focus primarily on the communication and interaction between objects and classes to achieve greater flexibility and organization in software systems. These patterns help in defining how objects are created, composed, and interact, thus allowing for the effective distribution of responsibilities among objects. Behavioral patterns such as the Observer pattern illustrate interaction by creating a one-to-many dependency between objects where changes in one object lead to automatic updates in others. This pattern is fundamental for implementing distributed event handling systems where objects need to be notified of changes in other parts of the system. Another example is the Strategy pattern, which encapsulates algorithms inside classes, allowing the behavior of an object to change dynamically at runtime by selecting different strategies. This promotes loose coupling by allowing the client code to work with an interface or abstract class rather than concrete implementations, facilitating easier switching between different behaviors without altering the client code.
The Command pattern encapsulates a request as an object, thus allowing for parameterization of clients with different requests, queue or log requests, and support undoable operations. This pattern decouples the object that invokes the operation from the one that knows how to perform it, which is particularly useful in UI frameworks where actions like clicks or key presses can be treated uniformly as commands. Additionally, the Chain of Responsibility pattern passes a request along a chain of handlers, with each handler deciding either to process the request or pass it to the next handler in the chain. This pattern is beneficial for scenarios where multiple objects might handle a request, and the exact handler is determined at runtime, such as in logging or event processing systems. Through these patterns, the GoF provides models for how objects can interact in a way that promotes reusability, extensibility, and robustness in software design.

1) Chain of Responsibility Pattern describes a system in which different objects each receive an opportunity to handle a request.
1) Chain of Responsibility Pattern describes a system in which different objects each receive an opportunity to handle a request. The request propagates up the chain until some object handles it. The Java 1.0 event model was based on this design pattern.

2) Command Pattern wraps requests from one object to another within a class.  This allows requests to be easily stored, queued, and undone.
2) Command Pattern wraps requests from one object to another within a class. This allows requests to be easily stored, queued, and undone.

3) Iterator Pattern describes an abstract means of passing through each of the elements in a collection, whether that collection is a set, a list, an array, a hash table, or some other data structure
3) Iterator Pattern describes an abstract means of passing through each of the elements in a collection, whether that collection is a set, a list, an array, a hash table, or some other data structure. It separates the enumeration of the collection from the way the collection is stored.

4)The Mediator Pattern is perhaps the most common and sueful behavioral pattern. The Mediator Pattern allows many different objects of the same or different classes that need to communicate with each other to each communicate only with a single, central mediator.
4) The Mediator Pattern is perhaps the most common and sueful behavioral pattern. The Mediator Pattern allows many different objects of the same or different classes that need to communicate with each other to each communicate only with a single, central mediator.

5) Observer Pattern allows an Observer object to notice and react to changes in an Observed object.Each observed object maintains a list of Observers and notifies each of them whenever its relevant state changes.
5) Observer Pattern allows an Observer object to notice and react to changes in an Observed object. Each observed object maintains a list of Observers and notifies each of them whenever its relevant state changes. The Java 1.1 event model is based on this design pattern.

6) Strategy Pattern allows different algorithms to be incorporated into different classes.The algorithms can then be changed dynamically at runtime and adjusted to fit the needs of a specific situation.
6) Strategy Pattern allows different algorithms to be incorporated into different classes. The algorithms can then be changed dynamically at runtime and adjusted to fit the needs of a specific situation.

7)Visitor Pattern lets you define a new operation without changing the classes of the elements on which it operates.
7) Visitor Pattern lets you define a new operation without changing the classes of the elements on which it operates.

Common Behavioral Patterns

Other behavioral object patterns are concerned with encapsulating behavior in an object and delegating requests to it.
  1. The Strategy pattern encapsulates an algorithm in an object and makes it easy to specify and change the algorithm an object uses.
  2. The Command pattern encapsulates a request in an object so that it can be passed as a parameter, stored on a history list, or manipulated in other ways.
  3. The State pattern encapsulates the states of an object so that the object can change its behavior when its state object changes.
  4. Visitor encapsulates behavior that would otherwise be distributed across classes, and
  5. Iterator abstracts the way you access andtraverse objects in an aggregate.

  1. The Chain of Responsibility allows an even further decoupling between classes, by passing a request between classes until it is recognized.
  2. The Command Pattern provides a simple way to separate execution of a command from the interface environment that produced it, and
  3. The Iterator Pattern formalizes the way we move through a list of data within a class.
  4. The Mediator defines how communication between classes can be simplified by using another class to keep all classes from having to know about each other.
  5. The Observer Pattern defines the way a number of classes can be notified of a change,
  6. The Strategy Pattern encapsulates an algorithm inside a class,
  7. The Visitor Pattern adds function to a class,

Common Behavioral Patterns - Quiz

Here is a short quiz on the material we have just covered.
Common Behavioral Patterns - Quiz

SEMrush Software 4 SEMrush Banner 4