Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Decorator Design Pattern in Python

Ankush Kumar Singh
Python in Plain English
3 min readJul 25, 2021

--

Decorators are widely used in Python and an extensively written topic. In this blog we will see what is decorator design pattern, when to use use it and how to code it.

To learn about basics of Python decorators, refer to the following article

By definition, decorator pattern attaches additional responsibilities to an object dynamically. In other words decorator pattern creates wrappers over an object such that object gets additional functionality defined by the wrappers.

Let’s take an example to understand decorator pattern. Suppose we have to develop an app that lets users create stories in different social media apps like Facebook, Instagram, Snapchat, Linkedin, Whatsapp etc. To implement this we can have a story creation function defined in the base class so that every social media platform’s child class can inherit the functionality from the base class.

Users love this app and want an additional feature to be able to create stories on multiple social media platforms at the same time. A simple solution for this is to create a story creation class for every combination of social media platforms, i.e. a class that creates story in Facebook and Instagram, another class that creates stories in Instagram, Facebook and Snapchat, and so on. This way each class is doing a very specific functionality. However, this leads to Python class explosion.

To avoid Python class explosion problem, we can use decorator pattern and following is the UML diagram which represents it. We have an interface Story and class ConcreteStory implements interface Story.

As per the definition of the decorator pattern, additional functionality to the ConcreteStory class object can be added by StoryDecorator class.

StoryDecorator can have multiple child classes. It is interesting to notice that StoryDecorator has both “is-a” and “has-a” relation with class Story.

Following is the Python code for decorator pattern. Classes Facebook, Instagram, Snapchat, Whatsapp and Linkedin define the story creation in the corresponding social media platforms. Functions show_off and professional wrap the ConcreteStory with different social media classes objects to add additional functionalities.

Following is the output of the code

Activating show off mode. Let's flood it on the platforms!
Creating story on ...
- Snapchat
- Linkedin
- Whatsapp
- Instagram
- Facebook
Let's keep it professional
Creating story on ...
- Linkedin

Thank you for reading the article. Following is the link other design pattern articles,

More content at plainenglish.io

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response