Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD
Play lesson

System Design Full Course - Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD

5.0 (4)
27 learners

What you'll learn

This course includes

  • 34.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

System Design Full Course Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD

Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD Transcript and Lesson Notes

حنجی کرے start so welcome back to the Koder Army in our LLD series today in this lecture we are going to start a new design pattern which is called Decorator Pattern this pattern is very interesting and you will get some new learning let's see how we can use this pattern and how our

Quick Summary

حنجی کرے start so welcome back to the Koder Army in our LLD series today in this lecture we are going to start a new design pattern which is called Decorator Pattern this pattern is very interesting and you will get some new learning let's see how we can use this pattern and how our

Key Takeaways

  • Review the core idea: حنجی کرے start so welcome back to the Koder Army in our LLD series today in this lecture we are going to start a new design pattern which is called Decorator Pattern
  • Understand how System Design fits into Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD.
  • Understand how Low Level Design fits into Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD.
  • Understand how High level Design fits into Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD.
  • Understand how System Design for Beginner fits into Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD.

Key Concepts

Full Transcript

حنجی کرے start so welcome back to the Koder Army in our LLD series today in this lecture we are going to start a new design pattern which is called Decorator Pattern this pattern is very interesting and you will get some new learning let's see how we can use this pattern and how our life is better in building LLD applications so let's start on the photo screen so first of all we know that what is our aim with this pattern so this pattern is called let's suppose we have an object ok, this is called OBJ1 I am saying that I need to provide additional responsibilities on runtime additional responsibilities that is, the functionalities what is currently doing is doing something else and I can change this on compile time on runtime right? so how do we understand this? for example, this object one, I call it a method which is called do something ok, so whenever I call this method this object returns me I did something ok, so let's see this is the return and what is the reply? I did something ok, now I want this output I did something I can change it dynamically so for example, now I will do something so this is something else for example, I did something great so how can I do this?

so for this you will think that we have inheritance right? we use that for some kind of object for functionalities we have to enhance so let's understand how we use inheritance think carefully what are the inheritance? we have a base class ok? and we have a child class and what does the child class do?

our base class is overwrite if we have a method in base class let's suppose run so the child class should be overwrite but let's suppose the base class is not declared it is defined and let's say this definition is run output running simple ok, now what I did? in child class I have overwrite this run method and it has output changed so running with skates so basically I can determine how to run and how to change the behavior so basically what I do? I will make a base class object and make a reference and it is equal to new now I will provide a class which is the base class and the child class which will be determined in the next line what will be the run call? and what will be determined here?

so basically if I provide a base class so running and if I provide a child then running with skates so this is the time to determine which method I can print a condition on a app if this condition is true then call this condition then make this base or make this child object I can also determine the run type then we need a new decorator pattern so first of all the first reason is which is inheritance is bad we read in the first lecture that inheritance is very expensive and it creates a multiple hierarchy, it creates a class and it creates many problems which we will see in today's and then we will see how we solve using this design pattern which is decorator pattern so let's start with an example and let's see how we solve it so the example is very interesting you all played Mario game what happened in the childhood? we had a character when the game starts Mario is very small and as soon as he gets some power in example, he gets a shoe and he gets some power so once Mario gets a height okay with one power he gets a gun shooting ability with the third power you will get a star ability which he takes and he gets destroyed but the star ability is only for some limited time so this is Mario's power if I am designing a game which has a character and I want to know that Mario has some power apps and I want to know that Mario has some power apps so if I am designing a game which has a character I am enhancing functionalities with 9 I want to do this for a dynamic, a behavior or a functionality change and hence if I add it to this what would I do? I made a class with Mario so I made a class with this method, let's say get abilities so the get abilities let's suppose, he would return Mario's character which has a character which can return let's suppose, a simple Mario's character which starts in the game now I want to enhance its functionalities so what will I do? I will think of making a child class so let's suppose, I made a child class Mario with height up so this Mario with height up has the same method which will override the get abilities okay now you can think how many classes can be made now the next class will be Mario with gundabillities and the next class will be Mario with star abilities so you are only looking for three classes so let's suppose, we want to make it scalable to make this design and not add the characters and to complicate this Mario will say that Mario has a gundabillities and Mario can also have a gundabillities in our game, Mario is not in the game so we should have a gundabillities so we should have a gundabillities first we always have a long time but we are designing Mario game let's suppose, we want to make it scalable that if Mario gets a flower then it is simple with gundabillities but not high-tabillities so let's suppose, it has no order that is, we can have a class called as Mario with gundabillities Mario with height up and we can have another class called as Mario with gundabillities now let's suppose, Mario has a new characteristic Mario has a flying ability so Mario has a new characteristic fly now we have to make new classes we have to make new classes we have to make new classes we have to make new classes Mario with fly and height up Mario with fly and gundab Mario with height up let's say, let's call it class explosion it means that we have so many child classes and we are making new child classes with every change and when you manage your application then it is difficult to work so this class explosion is because of inheritance we have already seen a strategy design pattern which has a long history and a long history which has changed and we have to change it so inheritance is bad and we always say use composition over inheritance we say that we should favor composition by changing inheritance and here in this decorator pattern what we should change and we should change its functionalities and we should change its functionalities and we should not have any problem like we have a class explosion or other problems in inheritance we solve this in a very interesting way let's see how we solve it so if we go back to our original example we said we have an object OBJ1 and we said that we call it method and we can reply something so we called it and we can reply it and now I want to enhance its functionalities and its characteristics or change it and when I say something I can also get something what is decorator pattern what is decorator pattern this object is OBJ1 decorate it or wrap it with another object which I will call decorator 1 okay whenever you do something call you call it decorator 1 and this decorator 1 will call it OBJ1 now we always reply it which is we can add something here and we will reply it so this decorator will reply and this decorator will enhance its function and give back the client and we can do something and for example I did something amazing so let us make it so what did the decorator do?

it did something and it did something and it did some amazing and we added it we did something amazing and then we will reply it and change the property and now we have decorated it and now we will see what we will call it so we will attach decorator 2 and now you will do something and then you will say decorator 1 so decorator 2 and decorator 1 and then we will reply it and then we will reply it so let us suppose this is the answer and this is the answer and this is the object and finally the decorator is the final decorator and I did something amazing so basically we are doing it we are enhancing the property without using this inheritance we are using decorator pattern but what is the decorator pattern actually you can attach it to the side that if I am saying I am decorating this base with another object okay, put it in color if I am decorating this base and I am decorating this base and I am decorating this which I am doing and I am adding it so I am adding it so one thing is that the decorator is also a object so basically this is the relationship so both are still in the same relationship because the one that I am adding will do something so that the decorator can behave like an object so this is a relationship so we are seeing it in the picture and here there is another relationship which is a composition relationship which is the haza so decorator also has a object why is this using this? this is using this as a to change the property to change the property that is, the use of inheritance not to change the property this is the use of inheritance to behave like that so this is the use of composition to extend the behavior to extend the composition I am adding a little bit of complicated but if you consider it as a UML then you will understand now you are just understanding that we have a base class okay? and we have a decorator what is the decorator doing? with the two different relationships with the base class and one is the so basically we are saying that decorator is a base class and decorator has a base class so that decorator can behave like a base class you have to behave like that to create a scene if you are inheriting the base class then you will behave like a base class so decorator is a base class now decorator has a base class that is, this decorator has a base class that is the base class now this can call the BK methods for example if the method is run and the method is run because it is overriding so this can call the BK run and then call the BK run if you think about it call the BK run and add it in it we did not know this I did something in the example if you are not clear then the mario will be clear and then you will run your mario example that we have a mario character and you can think about the additional power for example, you can have the property and you can have the height of the character like that so if we make this UML on a basic level then what will we do?

first we will say that we have a mario character but what is the character? so we are making a interface of it character okay so this is a mario character so this is a mario character now the character can be so different we have discussed in the last class that whenever we make an interface purely an abstract class then we denote it as i and this is not a purely convention but we remember that this is an abstract class and this is an abstract class so I will write this as an abstract so this is the same method called get abilities now this is a declaration because this is an abstract class we don't have any concrete implementation which we have one is mario character so we have done this first class so we have got the same method now let's suppose the return of its get abilities is normal mario character now we need to enhance its functionality for running time which is the object this is the object that will decorate so for that we need to make a decorator class so this is the way we make a decorator and this is the one decorator this is an abstract class or we can make a different decorator so let's make this this is our decorator and what we had said that the relationship is like a decorator and a decorator is a character so basically both are different it's interesting maybe you will see a character in a UML diagram now we have a decorator so this method will be the same if this is a relationship and let's say this is an abstract class so this is not defined by the decalers or by the child class now let's make a different decorator let's suppose a decorator is a height up and this method will be the same why it will be the same because the decorator inherits the i character get the abilities cut and this and this is the i character reference so basically a character's is the same we can make a different decorator for example gun power decorator and star decorator so we made a different decorator and star power decorator now you can think that the decorator is an abstract class so you can think how the decorator is providing the character and this is the character's character so this is the method of get abilities first this character's ability will be the same as the character's ability will be the same as the character's character with a height up this is the gun power decorator with the i character so this method can be called ch.get abilities and enhance this is the interesting how many orders can be used for example let's suppose i character ch equal to new sorry i name it new what am i doing first i will give some decorators which i am wrapping finally i will give the first i will give new height up decorator and this is the character i can pass new i did this height up decorator i did not have the height up because when i will call the decorator and this is the height up and this is the height up and this is the height up decorator i can stack a new gun power and this is the new decorator and this is the fly power decorator i will not have to make more more fly power and i will use new fly power decorator new i can use it in any combination i hope you will understand how we are using decorator and some functionality to enhance responsibilites we are using recursion and we are making a triple hierarchy recursion is calling recursive decorator i will create a new decorator and i will create a new decorator to enhance the height up decorator to enhance the run وہ کیسیو پارے ہیں کیوں کہ ہم نے اس کا uml diagram دیکھا تھا تو every decorator is a character تو یہ جو height up decorator اس میں میں character کا reference gesaktaom اور اپنے constructor میں یہ وہ character لیتا ہوگa تو یہ اپنے constructor میں gunpower decorator کو بھی لیسکتے کیوں کہ gunpower decorator is also a character اسی لے ہم نے a'a relationship use کیا تھا بس یہی ریزن تھا has a relationship تو ہم بس اس کو delegate کر رہے کہ get ability is pahle qaat ki is character ki pahle ability is call لیکن اس کے درہ بھیاف کرنے گلے تا کی میں اس کی constructor میں height up decorator ki constructor میں ek alak decorator pas karsakom تو یہ جو alak decorator hai it should be a character hai na یعنی ki it should be is a character تو اگر اس کے پاس ایک is a relationship hoga اس کو میں pas karponga اس کی constructor میں یعنی ki it should be pas karponga اس کی constructor میں اور میں ایک λمبہ height arkeetriye بنا ponga جسی ہم نے یہ بنایا تو میں نے character na diya star power is a character which is a Mario تو call ki asa oori oi اب جب بھی ہم اس Mario character this is a character which we call get abilities tuk kiya callod hoga obvisely sabse pahle baharwala decorator gunpower ghat ability is callod sorry star power sabse pahle is a new star power ki abilities callod ki ft ki ft ki ft ki ft ki ft ki ft ki ft ki ft کسی کے لگل کر کے لہوں, تو اس کے پاس ہے GEN-Power, تو یہ call کرتاوگa GEN-Power-Cabilities, GEN-Power-Bee-Same-Cathy옷i-Upni Ability-Functionmye, ki meray pas ju reference', ayu hiske gair-Tability call kair-Ki lau, تو یہ call kair-Taug, Hi-Tap ki, ASI-Hight-Up kair-Taug, Maariyogi, Maariyogi kuch-Return-Gar-Taug, اب اس کے RETURN, پe یہ کچھ add up kairke, یہ RETURN-Gar-Taug, و کسی return, پe یہ کچھ add up kairke, یہ RETURN-Gar-Taug, اور فیلی اُس پے کچھ ایڈان کر کر کہ یہ کچھ ریڈان کرتے ہوگا تو یہ کچھ طرح سے انہریڈنسی تو ہے بسیگ لیکھ ریکٹشن ریڈیشنشپ جس میں آپ لیڈر پیلے نہیں چیتاک جاتیوں وفتیسی کوئی بیس کے سیٹھ ہی تو تھی فیر آپ دبارے سے اپڑتاک جاتیوں so this is pure decorator pattern I hope آپ کو بیٹر ساہایا گا آپ ایک بر ہم اس کا standard UML diagram دیکھتے ہیں اور اس کے دیفینیشن دیکھتے ہیں اور فیر ہم جمپ کرتے اسی کے قوڈ پے so this is the standard UML diagram decorator pattern کہ ہمارے پاس سپ سے پہلے گیمسترٹ لاز ہوتی a component i component اور جس کے پاس ہمارے پاس بہت ساریکن Creed component ہمارے ایک زمپل میں مارے پاس ارفیک ماریو کرکٹر تھا پر آپ سوٹ سکتے ہو کل کم nouی نئے کرکٹر سنترڈیوس کر سکتے ہیں لیکھ سپوزم کچھ کیمز کو ایک ساتھ پیل آگے سونک کیریکٹر کو بھی ویمیں پہنٹرڈیوس کر سکتے ہیں تو اس طرح ہمارے پاس بہت ساریکن Creed component سوسکتے ہیں اب ان کن Creed component کو Dynamically اس کی رسم سبیلیٹیس کو ایڈان کرنے کے لئے ہم ایک دیکھوریٹر چیئے تو ہم ایک دیکھوریٹر بناتے ہیں اور دیکھوریٹر has a i component and decorator is a i component یہاں پہیر لیکششب دونوں والا follow ڈا ہے اب دیکھو decorator has a or is a or is a or is a or is a or is a i component a or is a reference right اب ہمارے پاس کییچر ایک decorator has a or is a so I have a concrete decorator a concrete decorator b بنایا اور ہمارے پاس اور بھی ترکی кон Creed decorator has a or is a sowsکتے ہیں اب ان دونوں concrete decorator pay اگر اس کے پاس method a method بھی تھا اس کے پاس بھی ترکی کی اس کو یہ inherit کر رہے تو same یہ بھی تو کهنا کئی کو inheritی کر رہے تو ان کے پاس بھی method بھی میں اس i component کو کل کر سکتے ہیں بیسگلی اس i component ke method a or method b کو کل کر سکتے ہیں اور بھی رہے بھی method a method b کو کل کر سکتے ہیں so اس طرح سے ہم show کر سکتے ہیں extended uml diagram اور اگر اس کے definition بھی دیکھلے کی اگر exactly decorator pattern کیا تاکہ تو یہ decorator pattern کی official definition decorator pattern attaches additional responsibilities to an object dynamically decorator provides a flexible alternative to sub-classing for extending functionality اب تو اپنے بہت سمجھا جایوگی ایک object pay ham additional responsibilities provide car dynamically اب یہاں پہت اگلی لنگی دیکھو کتھی ان tusting باتی کہتے کہ یہ ایک flexible alternative provide کرتے ہیں inheritance کیا کیا ہم اب اُس کو sub-classing نہ کریں صرف ان inheritance کو use نہ کریں کیا میں پہتا ہے ان inheritance ke baat sare cons et baat sare disadvantages et j rap کر دیں with additional deco-riders right اب ہم نے جو بھی اماریوال ایک zample ay اس کا فڑنفٹ سے مقود ڈکتے ہیں тew ایک zake same coda jubi ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ڈا ,ڈا ڈاready be called call lonely پھر ہم نے gun power の decoration ڈکڑیشن لگا دیا فیر اس کے بعد ہم نے اس کو پر star power の decoration ڈکڑیشن لگا و ہر line ڈکڑیشن پر پر انڈ کرتے ہیں کہ ہمارے پاس ہماریو کرریکٹر مر دکتا کیا سا تو ایک بر ہم سے run ڈکڑ کر دیکھیں فٹافٹ تو یہ دیکھو سب سے بہل ہمارے بیسی کرریکٹر ہے ماریو فید ماریو with height up فید ماریو with height up ویدگن فید ماریو with height up ویدگن with star power و last minute decorator کو distroy کر دیا تو یہ یہ پوری کی بوری کہانی decorator design pattern کی احب آپ کو اچھے سے سمجھا جایا گا اب ایک بر دیکھتے ہیں کہ ہمار کون کون سے scenario میں decorator pattern کو use کر سکتے ہیں تو دیکھو ایسے کون کون سے scenario زجس میں decorator pattern آپ use کر سکتے کون کون سے ایسا اپلکیشن ہے اچھا suppose آپ ایک tekst editor بنا رہے ہو جسے Google dogs جس ہم نے بھی بنایا ہی تھا پر تبتق ہم نے decorator pattern نہیں بڑا تھا ہم نے اس کو use نہیں کیا پر اگر آپ دیان سے سوچوں یہ ایک tekst editor میں ہمارے پاسےٹیکسٹ ہے اور اس کے پاس ہم کئی طراکے اس کی پروپٹیس کو انھیںز کر سکتے ہیں انھیں کہ decorate کر سکتے ہیں using bold right italics تو آپ سمجھ سکتے ہیں کہ یہ جو bold اور جو italics ہے یہ decorator کا گاام کرتے ہیں تو جب آپ ایک tekst لکھو اگر آپ سے bold کر رہے تو آپ اسے decorate کر رہے using bold اگر آپ اسے اس کے بعد italics کر رہے تو آپ اس کو bold tekst کو اور decorate کر رہے using italics تو آپ سمجھ سکتے ہیں یہ جو bold اور italics یہ decorated سکتے ہیں اور tekst کا سمجھ بیسکلس سکتے ہیں تو آپ اب اس کا easy leg diagram بناصلکتی UML سوچ سکتے ہیں کہ آپ ایک بیس tekst سوچے جائے گی right جو کی اپسٹیک class writer میں نوتیوگی اور اس کے بہت سمجھ رہے بہت سمجھ رہے بہت سمجھ رہے بہت سمجھ رہے اس کے لعب اور کنسے سمجھ رہے اچھا imagine کرو کہ آپ کے بسنا front and big form جو یہ کی user fill کرتے ہیں اور وہ back and پیدا اب back and پیدا آپ کو اس کی بہت ساری validation لگا آپ کو چک کرنا کہ e-mail ID جو اس نے دلیا وہ well-aid ہیں اس کے بہت سمجھ رہے اس کے بہت سمجھ رہے اس کے بہت سمجھ رہے اس کے بہت سمجھ رہے اس کے بہت سمجھ رہے

Lesson FAQs

What is Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD about?

حنجی کرے start so welcome back to the Koder Army in our LLD series today in this lecture we are going to start a new design pattern which is called Decorator Pattern this pattern is very interesting and you will get some new learning let's

What key concepts are covered in this lesson?

The lesson covers System Design, Low Level Design, High level Design, System Design for Beginner, System Design for advance level.

What should I learn before Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD?

Review the previous lessons in System Design Full Course, then use the transcript and key concepts on this page to fill any gaps.

How can I practice after this lesson?

Practice by applying the main concepts: System Design, Low Level Design, High level Design, System Design for Beginner.

Does this lesson include a transcript?

Yes. The full transcript is visible on this page in indexable HTML sections.

Is this lesson free?

Yes. CourseHive lessons and courses are available to learn online for free.

Continue Learning

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Lessons

Related Courses

FAQs

Course Hive
Download CourseHive and keep learning anywhere
Get App