Behavioral Design Pattern in Java
According to Wikipedia, in Software Engineering, behavioral design patterns are design patterns that identify common communication patterns among objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
Behavioral Design Patterns deal with the communication or interaction between Classes and Objects. The interaction between the objects should be in such a way that they are talking to each other and still they are loosely coupled. Loose coupling is the key to implementing the n-tier architecture.
So, the behavioral design pattern explains how objects interact with each other. It 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.
When to use Behavioral Design Patterns in Java?
In real-time applications, sometimes we want to change the behavior of a class and again we don’t want it to affect other classes of the project. For example, we have an Invoice class that currently applies taxes as 18%. Tomorrow if we want to add another extra tax. That means we are changing the behavior of a class. To solve such types of Behavioral Issues Behavioral Design Pattern comes into the picture.
Behavioral Design Patterns Examples
The following patterns are included under behavioral design patterns:
- Chain of Responsibility Pattern: This pattern creates a chain of receiver objects for a request. It decouples the sender and receiver of a request based on the type of request and allows multiple objects to handle the request1.
- Command Pattern: This pattern encapsulates a request as an object, allowing it to be parameterized with different requests, queued or logged, and supports undoable operations.
- Interpreter Pattern: This pattern provides a way to evaluate language grammar or expression. It involves implementing an expression interface that interprets a particular context2.
- Iterator Pattern: This pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Mediator Pattern: 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 and allows their interaction to vary independently.
- Memento Pattern: This pattern captures and externalizes an object’s internal state so that the object can be restored to this state later without violating encapsulation.
- Observer Pattern: This pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- State Pattern: This pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
- Strategy Pattern: This pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it.
- Template Pattern: This pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
- Visitor Pattern: This pattern represents an operation to be performed on the elements of an object structure. It lets you define a new operation without changing the classes of the elements on which it operates.
In the next article, I am going to discuss the Chain of Responsibility Design Pattern in Java with Examples. Here, in this article, I try to give a brief introduction to Behavioral Design Patterns in Java. I hope you understand the need for and use of the Behavioral Design Pattern in Java.