Good example of decorator pattern | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Good example of decorator pattern

Hi Every one mostly talks about decorator pattern with pizza example. I got idea about usage of this pattern as it helps avoid class explosion. However, Could someone please suggest a good example apart from pizza for decorator pattern?

8th Oct 2023, 12:36 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Answers
+ 1
Ketan Lalcheta , Please add a python tag. Two good examples of real-world uses of Python decorators I saw on YouTube were timing how long a function takes to execute and writing a log entry every time a function executes.
8th Oct 2023, 1:02 PM
Rain
Rain - avatar
+ 1
Ketan Lalcheta , OK. I see. "The DecoratorPattern is a pattern described in the [1994] DesignPatternsBook. ... This is not to be confused with PythonDecorators ..." -- https://wiki.python.org/moin/DecoratorPattern How unfortunate that the names are so similar.
9th Oct 2023, 4:28 AM
Rain
Rain - avatar
+ 1
Yeah. Thats why I had not tagged any language as this is generic to all. Also , i had no sample code for this question to make it specific to any language.
9th Oct 2023, 5:38 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Rain i am afraid it is not the same thing. Python decorators are related to functions. This is related to design patterns and class where we avoid multiple classes.
8th Oct 2023, 1:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Ketan Lalcheta , What is not the same thing?
8th Oct 2023, 11:02 PM
Rain
Rain - avatar
0
You are talking about decorator from python. This is the concept which is applied to functions. AFAIK, method / function decorator is limited to Python only. Is this correct? Now, coming to my query. My concern is not related to function decorators. This is a generic concept applicable to all object oriented programming language.
9th Oct 2023, 3:46 AM
Ketan Lalcheta
Ketan Lalcheta - avatar