DEV Community

jainnehaa
jainnehaa

Posted on • Updated on

Quick Guide to Behavioral Design Patterns

Design patterns are typical solutions to common problems in software design, which are tried and tested.
Each pattern is like a template that one can customize to solve a particular design problem in their code.
However, we need to make a check before applying if it is truly required or simpler existing code would do better.

Based on purpose, there are 3 categories of patterns :
Behavioral : dealing with communication between objects
Structural : define relationships among various entities (objects, classes etc), via composition, inheritance etc
Creational : related to object creation mechanisms

USE-CASES :

When one needs to decouple senders and receivers : Chain of responsibility, Command, Mediator and Observer Patterns can be used.

Chain of Responsibility handle or forward a request along a chain of potential receivers, based on run-time conditions.

Command normally specifies a sender-receiver connection with a subclass.
Polymorphism is important to Command. In this pattern, we can encapsulate a request into a stand-alone object that contains all information about the request and pass is as method arguments, log, delay or queue it's execution and support undoable operations.

Mediator reduces chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.

Observer distributes communication by introducing observer and subject objects, with One-To-Many Relationship.
It is similar to the publisher-subscriber pattern, but with the difference that there is no message broker or event bus as third party in Observer Pattern.
Also, the observers and subject communicate directly, mostly synchronously and subject maintains a record of the observers.

Memento represents the internal state of an object at a particular time which can be captured and restored later using encapsulation.
(Vocab Hint : 'Memento' means 'an object kept as a reminder of a person or event')

Chain of responsibility and Command Pattern represent requests as objects.
When we need to pass requests as token and need to invoke at a later time, Command and Memento Patterns can be used.

State Pattern alters an object's behavior when its state changes. The State pattern is closely related to the concept of a Finite-State Machine.

Strategy encapsulates an algorithm inside a class.

Template method describes the skeleton of a program in parent class. It then defer the exact steps of an algorithm to a subclass.

To select algorithm on run-time, Template method uses inheritance, while Strategy uses composition and delegation.

Interpreter implement a specialized computer language to rapidly solve a specific set of problems.

Null Object is designed to act as a default value of an object.

Iterators are used to access the elements of a collection sequentially without exposing its underlying representation.

Visitor defines a new operation to a class without change. It is a way to separate an algorithm from an object.

Iterator can traverse a Composite. Visitor can apply an operation over a Composite.

References :

sourcemaking
refactoring.guru
w3sdesign
wiki.c2
wikipedia
wikipedia
wikipedia
wikipedia
Blogs
wikipedia

Latest comments (0)