Python Design Pattern- Behavioral | Part 3

aps08
4 min readMay 21, 2022

--

Hello there, welcome back! Thanks to all of you, I got some awesome response on my last 2 articles of this series. If you are one of those people who haven’t read part 1 and part 2 of this series, I suggest you to give it a try.

In this article I will be covering (1) What are behavioral design pattern (2) It’s types with simple examples. I will give a short summary for each topic and some code so that you can understand it quickly in an easy way.

Keywords — Design Pattern, Python, Creational, Behavioral, Structural

What are behavioral design pattern

This design pattern deal with algorithm, and assignment of responsibility between different class and objects, such that they can talk to each other easily and still be loosely coupled.

For example, they are good practices when you are tempted to implement a solution for busy waiting or when you have to load some unnecessary code for a specific use case.

Types

Observer — The observer pattern establishes one to many relationship between a subject and multiple observers.

Observer
Observer

The above example replicate, notification service to everyone in a factory, when there is a change in the temperature of the core. As soon as there is a change in a temperature in Core 1, the update function is triggered to inform all members.

The observer pattern is easy to establish relation between different objects, it also describes the the coupling present between objects without adding or removing the subjects and observers.

Visitor — It allows adding new features to an existing class hierarchy without changing it.

Visitor

In the above example, the visitors visit the house class using different objects. Hence adding new visitors is very easy making changes inside the house or visitor class.

In this pattern, it is easy to add any entity in this design pattern, and multiple version of the same behavior can be added to the same class. Also, if there is a change in the logic, then we only need to make changes to only one class.

Iterator — It allows the client to have sequential access to the elements of an aggregate object without exposing its underlying structure. Used specially with long linear and non-linear data structure.

Iterator

This design pattern helps us iterator through a long list of object, specially linear and non-linear data structure quickly.

This pattern supports variation in traversal of the collections, and passing a new iterator or collection on client code does not break the code and can be installed easily on it.

Strategy — It offers a family of interchangeable algorithm to a client. Mainly used for dynamically changing the behavior of an object.

Strategy

As you can see in the above example, we are passing function to the strategy class, and later on we are calling execute function after changing the name variable.

Using this pattern a family of hierarchy can be defined and can be used interchangeably, it can also help us change the strategy during run time, with that we can avoid the use of any switch of if-else ladder.

Chain of responsibility — It opens up various possibilities of processing for a given request. It decouples the request and its processing. The objects in the chain decide whether a request should be passed to next object in the chain or not or which object should process this request.

Chain of responsibility

This design pattern is applicable when, you want to decouple a request’s sender and receiver, when request handler should be decided during run-time, or when you can specify a specific handler for a request. This design pattern allows adding or removing responsibility from the chain or object very easy, by changing a member between the chain. It also increases the chance of processing a request.

Thank you for reading this article. 😃😃😃 Of course, this information is based on my knowledge. If you like this article give me a clap and feel free to connect with me on Twitter, GitHub and LinkedIn.

My old articles:

  1. Python Design Pattern- Structural| Part 2 [Technical]
  2. Python Design Pattern- Creational | Part 1[Technical]
  3. Working with git for noobs [Technical]

--

--