Quick Guide to Behavioral Design Patterns
Tags: java, design, behavioral, patterns
Published: 2022-08-10 14:10:00

Design patterns are typical solutions to common problems in software design, which are tried and tested.
Each pattern is like a <u>template</u> 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 

<u>USE-CASES :</u>

When one needs to **_decouple senders and receivers_** : Chain of responsibility, Command, Mediator and Observer Patterns can be used.

<u>Chain of Responsibility</u> **_handle or forward a request along a chain of potential receivers_**, based on run-time conditions.

<u>Command </u>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.

<u>Mediator</u> reduces chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them **_to collaborate only via a mediator object_**.

<u>Observer </u>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.

<u>Memento </u>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.

<u>State Pattern</u> **_alters an object's behavior when its state changes_**. The State pattern is closely related to the concept of a **_Finite-State Machine_**.

<u>Strategy</u> **_encapsulates an algorithm inside a class_**.

<u>Template </u>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_**.

<u>Interpreter </u>implement a **_specialized computer language_** to rapidly solve a specific set of problems.

<u>Null Object</u> is designed to act as a **_default value_** of an object.

<u>Iterators </u>are used to **_access the elements of a collection sequentially without exposing its underlying representation_**.

<u>Visitor </u>defines a **_new operation to a class without change_**. It is a way to separate an algorithm from an object.

<u>Iterator </u>can **_traverse a Composite_**. Visitor can **_apply an operation over a Composite_**.

<u>References :</u>

[sourcemaking](https://sourcemaking.com/design_patterns)
[refactoring.guru](https://refactoring.guru/design-patterns)
[w3sdesign](http://w3sdesign.com/index0100.php)
[wiki.c2](https://wiki.c2.com/?GangOfFour)
[wikipedia](https://en.wikipedia.org/wiki/Design_Patterns)
[wikipedia](https://en.wikipedia.org/wiki/Behavioral_pattern)
[wikipedia](https://en.wikipedia.org/wiki/Structural_pattern)
[wikipedia](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern)
[Blogs](https://betterprogramming.pub/observer-vs-pub-sub-pattern-50d3b27f838c)
[wikipedia](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern#Message_filtering)