Design Patterns?-?[3] Behavioral
In the previous article [2] Structural Design Patterns, we discussed in details the some of the most famous example of the Structural Design Patterns. Now we will continue with our next design pattern.
[3] Behavioral Design Patterns
Behavioral design patterns are design patterns that focus on communication between objects, defining a way for objects to interact and collaborate with each other to accomplish a common goal. These patterns are concerned with the flow of control between objects and how they interact to produce a desired outcome.
[1] Chain of Responsibility, Command, Mediator, and Observer, address various ways of connecting senders and receivers of requests:
[2] Strategy lets the class vary its behavior when its strategy gets changed at runtime.
[3] Template Method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
[4] Visitor lets you add new operations to classes of different objects without altering their source code.
Each of these patterns provides a unique solution to common problems that arise when designing software. Understanding the different behavioral design patterns can help you choose the best approach to solving complex problems and help you improve the overall design of your code.
(A) Chain of Responsibility Pattern
The Chain of Responsibility pattern is a design pattern used to handle requests or to forward requests to the next handler in a chain of handlers. The pattern provides a loosely coupled way of sending requests to one or more objects, while decoupling the sender and receiver of a request.
Here is an example implementation in Kotlin:
In the example, ConcreteHandler1 and ConcreteHandler2 are the concrete handlers in the chain. When a request is received, each handler checks if it can handle the request. If it can, it handles it and returns. If it cannot, it forwards the request to the next handler in the chain. The chain ends when the last handler in the chain cannot handle the request and returns.
The Chain of Responsibility pattern is useful when you want to decouple the sender and receiver of a request, and when you want to allow multiple objects to handle a request. The pattern also makes it easy to add or remove handlers from the chain, without affecting the other handlers in the chain.
* Disadvantages:
The disadvantages of the Chain of Responsibility pattern are:
Despite these disadvantages, the Chain of Responsibility pattern is still a useful design pattern in certain situations where decoupled communication is required between objects and multiple handlers can handle the same request.
(B) Command Pattern
The Command pattern is a behavioral design pattern that allows objects to encapsulate requests as objects and pass these requests to different receivers. This pattern provides a way to decouple the senders of requests from their receivers, allowing the senders to be ignorant of the receivers and the receivers to be ignorant of the senders.
Here's a simple example of the Command pattern in Kotlin:
In this example, the LightOnCommand and LightOffCommand classes implement the Command interface and encapsulate the request to turn on and off the light, respectively. The Switch class uses a list of Command objects to keep track of the on and off commands. When the onButtonWasPushed or offButtonWasPushed methods are called, the appropriate command is executed.
The Command pattern allows you to encapsulate requests as objects and pass them around, which can make the code more flexible and easier to maintain. Additionally, the Command pattern makes it easier to implement undo and redo functionality, as the history of commands can be kept in a stack.
* Disadvantages:
The disadvantages of the Command pattern include:
(C) Mediator Pattern
The Mediator pattern is a behavioral design pattern that provides a centralized communication channel between objects. The goal of the Mediator pattern is to reduce coupling between objects and promote loose coupling. This is achieved by encapsulating the communication logic between objects within a mediator object.
Here is a simple code sample of the Mediator pattern in Kotlin:
In this example, the ChatRoom class acts as the mediator, which handles communication between the User objects. The User objects communicate with each other through the mediator, which encapsulates the communication logic and reduces the coupling between the objects.
Note: This is just a basic example to illustrate the concept of the Mediator pattern. In a real-world scenario, the mediator would typically handle much more complex communication between objects.
* Disadvantages:
The disadvantages of the Mediator pattern are:
(D) Observer Pattern
The Observer pattern is a behavioral design pattern that defines a one-to-many relationship between objects, where one object (the subject) is being observed by multiple objects (observers). When the subject changes state, it notifies all its observers, who then update their state accordingly.
The key elements of the Observer pattern are:
Here is an example of the Observer pattern in Kotlin:
In this example, the WeatherData object acts as the subject and the TemperatureDisplay object acts as the observer. The TemperatureDisplay object registers itself with the WeatherData object and gets notified of any changes to its temperature. When the temperature changes, the WeatherData object notifies all its observers, and the TemperatureDisplay object updates its display accordingly.
* Disadvantages:
The disadvantages of the Observer pattern are:
(E) Template Pattern
The Template pattern is a behavioral design pattern that defines the skeleton of an algorithm in a method, called a template method, which defers some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
Here’s an example of the Template pattern in Kotlin:
In this example, the Game class defines the template method play(), which calls the three methods initialize(), startPlay(), and endPlay() in a specific order. The Cricket and Football classes extend the Game class and implement the three methods. When the play() method is called, the algorithm is executed and the steps are performed in the order defined in the template method.
* Disadvantages:
The disadvantages of the Template Method pattern are:
#softwaredesignpatterns #designpatterns?#designpattern?#android?#androiddeveloper?#androiddevelopers?#androiddevelopment?#androiddev?#androidstudio?#androidengineer?#androidapplication?#androidapp?#kotlin?#kotlindeveloper?#kotlinandroid?#java?#javaprogramming?#javadeveloper?#software?#softwaredevelopment?#softwareengineer?#softwareengineering?#softwaredesign?#softwarearchitecture?#coding?#coders?#pythonprogramming?#csharp?#golang?#webdevelopment?#phpdevelopers?#phpdevelopment