System Design Full Course Prototype Design Pattern | UML + Code | System Design
Prototype Design Pattern | UML + Code | System Design Transcript and Lesson Notes
حن جی تو کرے شاڈ Welcome Back Quarter Army our Celety Series and today we are going to see a new design pattern whose name is Prototype Design Pattern now see this design pattern is very simple but a very specific problem is solved and what is the problem first we will discuss and how
Quick Summary
حن جی تو کرے شاڈ Welcome Back Quarter Army our Celety Series and today we are going to see a new design pattern whose name is Prototype Design Pattern now see this design pattern is very simple but a very specific problem is solved and what is the problem first we will discuss and how
Key Takeaways
- Review the core idea: حن جی تو کرے شاڈ Welcome Back Quarter Army our Celety Series and today we are going to see a new design pattern whose name is Prototype Design Pattern now see this
- Understand how prototype fits into Prototype Design Pattern | UML + Code | System Design.
- Understand how design fits into Prototype Design Pattern | UML + Code | System Design.
- Understand how pattern fits into Prototype Design Pattern | UML + Code | System Design.
- Understand how code fits into Prototype Design Pattern | UML + Code | System Design.
Key Concepts
Full Transcript
حن جی تو کرے شاڈ Welcome Back Quarter Army our Celety Series and today we are going to see a new design pattern whose name is Prototype Design Pattern now see this design pattern is very simple but a very specific problem is solved and what is the problem first we will discuss and how this prototype is solved by design patterns then we will see this is very simple design pattern you will understand it very easily and you can use it first because many languages have its support already now you will not know it is a design pattern as you will not know about it and you will not know about the prototype design pattern or you will not know that it is a design pattern but you can use it so let's go to the screen and see the design pattern now let's see now let's see our prototype design pattern actually solve the problem okay now consider you have an object okay we will not say it again okay now obviously a object will be created from a class you will have a class now this is an OB1 object so we can call it a class let's call it a capital okay it will create the object 1 so this will be a class and what you will do to make this class you will write A star A equal to new A right if you are in Java you will not place this star here and you will place it in C plus okay because you have to allocate it in a heap memory so what will happen in a heap memory a chunk of block will be allocated you already know okay but I am saying that I created this once again okay now I know that another object is if A1 is let's call it OB1 A1 let's call it a object now from this point I want another object which is the name of A2 so what will I do? I will have to do A star A2 equal to new and I will have to do this again okay now I am saying I want another object so I will do this again okay now I am saying A star A3 equal to new A so this is the time we used to do this right if we had to create a new object then every time we used to call a new A that is, we used to say in a heap we used to allocate another chunk of memory whatever will be available in A we will start in A's constructor and our A object will be created right so what was the problem? so see many times that there is no object it is a class whenever you create an object then create that object it is a very expensive operation okay what is the meaning of an expensive operation? look, it is the meaning of an expensive operation it is possible that whenever you create this class then you have to establish a connection with the database in which you have to spend a lot of time right or you have to do a complex calculation right from any reason create this object it is very expensive I said that to establish a connection in the database to establish a complex let's say to calculate a calculation let's say to load a complex file okay so basically it is a very expensive operation which has a very long time right what happens here for the cost?
so let's say it is a 2-second object creation or 3-second object which is a lot of time right so every time you cannot create this object and you also need a lot of objects so see today we are going to take an example we are going to take the NPCs you will play video games right so there are many NPCs what happens? non-player character right computer-oriented player computer-generated player I mean right they are not real players so you know you can imagine there are many non-player characters which they are not so successful but they are going in the background for example, if we play GTA so many people right who are going in the same way right they are all NPCs right I am saying imagine if we want to load a game then we have to create many objects we have to create many characters and we have to generate a NPC complicated operation right let's say we have to load some mage and we have to query the data whatever we want to do but we have to create one object it is a complicated it is a little expensive so if we have to create so many objects then we know how many memory we have to get how many time we will consume then everything will be complicated so where we help prototype design pattern what does it say? prototype design pattern solves this problem that you once you create one object you created this object and then OB1 so if you want to create a new object then you don't need to create a new object then you can create a copy you can create a copy then this helps us prototype design pattern that you have to create a new object that you have to create a copy and then you have to clone it so that you have to do a new object because I had told you to create a new object but to create a copy it is very easy you have to copy the values now you have to do all the queries and the complex operations you have to do you have to do less operations in the code this is prototype design pattern now we have to do and understand it so what I said let's say you have a NPC okay so a character you have a computer and a director now we have to generate this NPC to create this object this is an expensive operation so here I write expensive operation okay now we have to do something that is let's say expressions it will be a connection with a database it will be a connection but whatever it is to create an expensive operation so what I will do is that expensive operation which will take so much time to create this object once in a time from n1 I will make it new NPC and once I get n1 now I will not construct it if I want the second object from npc n2 I will not say from n2 I will say n1 I will give my clone so n1 has a method I will call it I will call it let's define it so n1 I will give my clone n1 will give my clone and my work will be done how to create a clone how to create a clone how to create a clone now we will see it okay if you have read about copy construct then prototype design pattern is used okay you know what is copy construct copy construct okay so first you will know the constructor we have default constructor this constructor is not an argument so let's say you have a class A class A so the default constructor will see empty this is your default constructor now we have parametrized constructor parametrized whatever you take in this constructor let's say a integer a b so in this constructor you take this parameter and then we say a new constructor and another new constructor okay what is the work it is the work it is the work exactly what we are discussing in the prototype that we have class A we want to create a clone then we will take a copy constructor in which we will take another reference again a reference of A and we will create this copy now how we will create we will see in the code if you want to make a copy then we will use exactly the entire prototype design pattern okay I told you the problem that if you want to make some objects it is very expensive and we can't create the object once we create an expensive operation and then we will copy it and clone it and then we will create clones okay so once we see then we start with our UML then I told you that we have a class with the name of NPC okay so we have created our NPC okay now what can we do in this NPC class so I said what I did I said a player so what can we do we can do something basic we can name it okay we can have a health in the game we have an integer we will have a string so string name okay we can have a health what else can we do we can have a power so let's say we have a power then we will have a power so we can have some basic details this along along this we have a copy NPC which will name and power initialize so this is our normal class now what I said I have to give it its clone right I have to give it its clone so let's implement it first I will create a game because I want a method okay so first first I take a method clone okay now what is the method basically any NPC or whatever class is using it it will create a clone now where is the method where is the clone so what I do for this I create another abstract class which I name or prototype or cloneable I can also keep it so it looks more like clone okay so I name it cloneable we can also keep it so I created a cloneable class this is our abstract class okay there is only one method and this method doesn't have any argument and it is only declared here because it will be a real cloneable or a concrete cloneable okay now you understand that the cloneable I have created an abstract class which has only one method and it doesn't have any argument I have just made that whatever this abstract class will inherit I will understand that the object is cloneable that means it can be created so that is that that is that the client will talk about this cloneable so that the place of this cloneable can give us a concrete reference such as NPC or I can make something else or I can make something else so basically you understand prototype design pattern this is the abstract class it has no other work it is acting like a marker in fact it has a proper name it is called marker interfaces okay such interfaces which are called this is the concrete class which is the purpose of this particular class such as this interface is cloneable or the abstract class C++ so this is the pure abstract class cloneable this is the only thing that whatever it will inherit will be cloneable okay so this is the work of marker only this is mark which is cloneable as NPC is cloneable why? because this has a clone method and it has a parent class now how will it be done?
it will be done here there is a more copy NPC again let's take NPC let's do this what will be done? what will be done? the values name, health and power it will copy how it will be NPC and equal to this NCO N. where is this health?
what is this NCO N. health so whatever you did in this reference it copied the name and power and this is the copy constructor now what is the clone method that is the implementation this does not return and new NPC reference by giving it this that is the copy that is stored in this reference and it returned it who is returning the client who is calling our NPC is simple this is the work of the small purpose and the interface and the clone will be available to clone or to override a clone method and to create a new NPC but all the values will not be the name and power but you need to create a db connection for 5 seconds to be a object but if you need to do the copy that is used to calculate the value for the connection and the calculation and the values are used to use to use to use to use to use to use to use to use to use to use to use to create a new object and the nPC characters in any real life and any real life and any real life and any real life and any real life and any real life and every thing will be for us to create a new object and every thing will be for us to create a new nPC and every thing will be for us to be میں پھر گیٹر گیٹر گیٹر کچھ کرتے تھا کرسکتا ہوں تو میں کیا کروں گا n2 x ceter ہوگا let's say set power اس کو میں کچھ اور set کر دوں گا Right تو میر کو بہت تھوڑا بہتی تو change کرنا ہوگا تو اس کو میں set ڈرکے تھوڑ change کرسکتا ہوں پر اگر ما لوگوں جو پورا کبھر آبجیکٹھ ہی change کر رہے اس کس میں یہ جو Prototype Design Pattern یہ واہلگے گا بھی نہیں obviously اگر ہمارے اس سے اگر بہت اگر بہت اگر اگر بہت اگر اگر ان کی ساری value zi ڈرکے تو اس کس میں تو ہم اس کو clone نہیں کرتا ہے و رگر کر بھی رہا تو کو 5 دا نہیں ہے کیا میں بہت سو اس کی ساری set اگر clone n and prototype design pattern صرف اسی use kees kele very good جاہاں پہ جو آپ کے اگر اگر اگر small t objects ay وہ آپ بہت very good d similities رکتے اور کو کس از وارف کرتے using copy constructor r تو وہ کیا کرتے copy constructor اور اپنے پچھلے value zi جو بھی اس نے پہلے بنائی تھی اسے copy کر لے تا اپنے نئے object کو r یعنی کیا کرنا میں n 2 بنانے تو ہم n1 کا clone call کر دیں گے تو میں n2 بن کیا جاہاں جاہاں یعنی ki پہلا بس 1 object بنانا پڑے گاہاں میں اور باکی ساری او سے copy ڈا r تو یہ ہے اس کو بس چینج کر دیں تو میں نے کیا کیا پاورفو لیان کا بس def strong ڈا پہلے اس کا def ڈو تھا اس کا def 5 ڈا تو اب یہ پاورفو لیانہ اس کی کچھ کچھ خاص value change nuisive defense change juhapa ki n تو obviously change h ڈا اور میں اس کو disc کر رہ اس کے لئے میں کو اس کا n n pc بنانہ پڑا اب ایسے اگر میں saw aliens بھی بنادوں پر بھر بھر اس کو t caul d t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t mak s t t t t t t t t t t t t t t t t t t t t t tit t t t t t t t t t t t Wennて� tt mt ti t t t t t t t t t t t t t Reo n infectious infected galaxy Ö cushion d看到哦 species genome de.. جو کلونے بل کو انہرٹ کرتے ہیں جو اس کے پھر 4 value sain name, health, attack or defense جو پہلے تھا اس نے again اپنا Expensive Constructor نہیا جس میں یہ سب جو values を set کرتا نہ تو وہ بھو چھوڑ Expensive ھتری گے سے set کر پا تھے again یہاں میں نے کیا کرا تھا میں نے کہتا کہ یہ کچھ complex database call ہو رہوں گی, کچھ calculation ھوڑھوں گی تو اب مالوں یہاں بھی سب کچھ ویزہ بھوڑ ہے right database کو call جاتی ہے right complex calculations ھوڑھی اور تب یہ object بن پا تھے پر ایک بار object بن گیا نہ فٹ دیکھو میں نے کیا کیا تو دیکھو میں نے copy constructor لیاں پے اب وہ کیا سیاں again constructor میں کیا ہوتا کلس کا نام اور یہاں میں نے کیا لیا اسی class کا ایک اور reference لیا یہ دیکھو NPC, adder اب جب میں اس کو build کروں گا تو دیکھو کیا سا بل کروں گا name میں دال دوں گا adderka name, health میں دال دوں گا adderki health, attack میں دال دیکھ adderka attack, defense میں دال دوں گا adderka defense جو جو values پہلے سے تھی پہنوں values کو میں نے اٹھالیا اور میں نے clone گر دیا یعنی کی مجھے اس constructor کو call کرنے کی ضرورتی نہیں پڑی مجھے اس object کو دبارہ سے create نہیں گرنا پڑا new operator نہیں لگا نہ پڑا right اب میں نے new operator نہیں لگا ہے اس کا مکلپ میر کو اس constructor میں گھوسنا نہیں پڑا میری fall to me ju expensive operations ہوتے وہ ساری چیزے نہیں ہی اور میرا time بج گیا میری let's say space بھی بج گیا right تو اب یہ مرا ہو گیا copy constructor اب ہم نے کہتا ہمیں اپنے clone method کو یہ جو ہمارا cloneable سا رہس کو overwrite کرنے تو اب نے اس کو overwrite کیا ہم نے simple return کیا new NPC اب جب آپ clone method call کر ہو گے تو اب کیا کر ہو گے پہلے heat make memory بناؤ ہو گے new NPC پر میں نے کہا میں اس constructor کو call نہیں کر رہا گیا کی اس کے through اگر میں گیا تو مجھے بہت سارے let's say database call کرنے پڑے گئے کلگلگلیشنز کنی پڑے گئے میں نے سیدہ star this pass کر دیا ہی دیکھو اپنا reference pass کر دیا اور میری reference میں name, health, attack, defense in sub-key values وہ ست ہو گئے جو پچھلے والے گئی جو پچھلے والے گئی جو اب جگ بنہتا اس کی جو values وہ ست ہو گئی تو مجھے دوبارہ سے اس constructor کے اندر جانے کے ضرط نہیں ہے میں سیدہ copi constructor call کر ہو گا new NPC this کو دیکھے تو میں نے یہ والا constructor call کر دیا اب دیکھو اس نے کیا return کیا new NPC اپنے NPC کا reference جو ہم نے stour کرا لیا cloneable میں write parent class reference child class وہ stour کر سکتے تو ہم نے کیا finally return کیا cloneable got object return کیا write اب اس کے پاس بھی a discribe میں thalagia same as you have described کچھ نہیں کرتا جو بھی اس کی values اُس کو screen پر ان کرتے با کی ہمارے پاس کوج گیٹر سیٹر سیٹر سیٹر ہم نے کیو دیا یہ کہ آپ کو thola بات tweak کرنا ہو let's say the health a little change now attack, defense, defense, defense تو ہم نے اس کی setter اب دیکھو دوارہ سے same problem ہم نے an alien whose key 35 or 2 یہ ساری values تھی ٹھیک ہے فرم نے ایک an alien copy بنائی an alien clone call کیا اس نے میں کیا دیا ہوگا اس نے میں cloneable got reference اس کو میں dinami keli cast کر دیا NPC ke reference اور ہم نے ملگہ an alien copied آپ کو بات ایسی بلس بلس میں اگر ہمیں cloneable got reference ملگہ بات ایرینت رفرنس ملگہ اور ہمیں اس کو child get reference میں دائنامی cast use کرنا پڑھا simple اب ہم نے کیا an alien copied کو describe same ہم نے an alien copy 2 بنایا اس میں دیکھو بلکل same values کیا ہم نے کچھ بھی چیجنگیا پر ہم نے ایک more 2 بنایا اس کو بھی ہم نے کیا an alien clone اور dinami cast کر دیا NPC تو ہم نے an alien copy 2 بنایا پر میں نے اس کا nm اور اس کی health کر دیا میں نے name alien سے powerful alien کر دیا اور health سے 30 50 ٹ کیاbars を 6 بنایا کا ٩ mé کا ٩ mé کا odds declaration cier unnecessities bek summer ova need نہیں بڑا تو ابھی ہم دیسکس کر لے تھے کیا ہوتی شیلوコپی اور دیپ کپی ہم نے کہا کہ ہم نے نے کپی Construct میں رکھا تھا تو ہماری جو NPC کی قلاز تھی Klaas NPC Rite اس میں ہمارے پہ سے کپی Construct Rite NPC جو کیا کرتا تھا ایک اور NPC کا Reference لیتا تھا لیٹھ سے N Rite اور اس میں جو بھی ہا value ڈی دی اس کو کپی کرتا تھا اب نے ہمارے نہ 2 طرح کی کپی ہو سکتے کسی بھی language میں یا تو شیلوコپی ہو سکتی ہے یا دیپ کپی شیلوコپی کا مطلب کیا ہوتا دیکھو یہاں پہ تو کیوں کی ہمارے پاسر فرمیٹر بڑیٹا دیپ سے کی انٹ ایکس تھا انٹ وائی تھا کچھ بھی آپ من سکتے ہو ہمارے از پاورتی وگرہ وگرہتا ابھی کے لئے میں ایکسور وائی مان لیتے ہیں تو میں نے کیا کر دیا تھا ہم نے سے کیا تھا کی دیس داٹ ایکس کو کر دو این کا ایکس اور دیس داٹ وائی کو کر دو این کا وائی تو یہ جو ہم نے کپی کیا ہنا یہ کرنے کو تو ہم نے صرف شیلوコپی کیا ہے لیکن ایکچلینا یہاں پہلیوز کپی ہو گئی کیوں ہی دیکھو آپ کو پتے جو بھی پرمیٹے ویلیو دو تھی ہیں وہ ہماری بھائیسٹور کیا ہوتی ہیں تو لیٹسے ہمارے پاس ایک انٹ ایکس ہے مطلب ہمارے پاس ایک ممری ہو گئی ایکس جس میں ایک ویلیوز دو ایکس سی 20 اگر ایکس کو کپی کرتا ہوں تو ایکچلی اس ویلیو کو کپی کرتا ہوں کسی اور لیٹسے ممری انڈیکس پی ایکس تھا تو لیٹسے اس کا لیٹسے ممری ادرس تھا 1001 اور اس کا ممری ادرسے 1002 تو ایک ایک ادرس پی کپی کر لیا اب جب بھی میں اسمکھو چیج کروں گا اس کو 20 ادرس تھا کروں گا وہ اس میں رفلیکٹ نہیں ہو گا یا آپ کو پتای ہو گا تو یہ ہو دیکھا آپ کا اپریمری ادرس تھایپ کیسات پر جو آپ کا نون پریمری ادرس تھایپ سوٹے ہیں اگر یہ جو یہاں پے انٹیجر ایکس اور وائی کی جگا اگر میں آپ دو بارہ سے دو بارہ سا سپیس جیے کے بناؤ اب دیکھا آپ میں بھنھ سوڑھ سا سپیس دیا میں نے کہاں لیٹسے نپیسی کلاس میں نہ کوئی نون پریمری ادرس دیٹرس تھایپ بھوتے لیٹسے ہمارے پسے کچھ سٹوڑنٹ کلاس ہوتی سٹوڑنٹ کا رفرنس ہوتا S1 ایسےی لیٹسے اور کوئی بھی آپ رایدنم کلاس مانڈویکٹیچر کلاس ہوتی تو ٹیچر کا رفرنس ہوتا ٹیپن اب آپ کو لوگے نپیسی میں سٹوڑنٹیچر کیا کر رہے پر میں نے بس اگسامپل کے لے مانگے چل رہوں کہ ہمارے پاسنا کچھ آبجیکٹ سے جو آپ نے خود سے بنایا اور یہ کیا ہے یہ ہمارے نون پرمیڈیٹر ایپسے ہیں اگر اس کا ہمارے پاس آبجیکٹ ہوتا اور ہم جو ہمارے کافی کنسٹر ہوتا اس میں ہم سیم کوڑ دیگ دیگ دیگ اور ہم ایسے لگ دیگ دیگ S1 کو کر دو یعنی کی ہم بسکلی ایسے کر دیگ دیس دوٹ S1 کو کر دو این کا S1 تو یہاں پہ ہمارے دکتا ہوتی کیو دکتا ہوتی کیا ہمارے کیا ہمارے ہیپ میں یہ جو ایک ممری رزا بو یہ S1 کے لئے نا وہ ممریق رفر کن کر رہے یہ S1 포인ٹر رفر کر رہے یہ نا کی ہیپ میں ایک ممریے جو S1 variable رفر کر رہے میں نے کیا کہا ایک دوسرا آبجیکٹ بنایا کسی بسکلی آبجیکٹ بنایا ایسے میں ایک آبجیکٹ بنایا اور میں نے کیا کہا یہ بیسی کو رفر کرے گا یعنی کیا کر میں نے اسم کو چیج کیا نہ تو وہ اس کے لئے بی چیج ہو جائے گا تو یہ سا بہتا ہے پتا ہے about shallow copy تو یہاں پہ اس کوم shallow copy کہتے بسکلی ہم نے copy نہیں کی ایک چلی میں ہم نے دونوں رفرنس کو same heap memory میں 포인ٹ کر بایا جا ومیں بھی بلکن same چیز ہوتی کہ ہمارے بھی ممری same رہی اور ہم نے دوں رفرنس کو بہتا ہے پہتا ہے پہتا ہے پہتا ہے بلکی deep copy کیا ہوتی deep copy کا ایک چلی heap میں سے یہاں پہ جو ممری تھی اس کو دوبارہ سے ہم نے copy کیا ایک ایک الگ heap memory میں بسکلی ایک الگ memory space میں تو heap تو بہتا ہے بہتا ہے بہتا ہے تو یہاں پہ لٹسے کو کو بجیک تھا ہم نے اس کو ایک چلی کہیں اور بھی کو بھی کو بھی اور اس کا reference S1 الگیا اور اس کا reference S1 الگیا یہاں اپجیکٹ ہے اپجیکٹ ہے تاکہ اس میں اگر کوئی چینج ہو تو اس میں رفرنٹ نہ ہو اور اس میں اگر چینج ہو اس .S1 Equal to new ہمیں ہمیں نیو اپرےٹر لگا نپٹے گا کیا نیو student براہنے جا رہا اور اس student میں ہمیں ہمیں ہمیں این کا S1 پاس کرنا پڑے گا بسکلی ہمیں ہمیں نیا student بناہیں اس وائلے student کو ایک نیا student کلیت کریں گا تو اس طرح کا کچھ ایک چیستی جو میں دیسکس کرنیتے ہم نے وہ بھی کر لی باکی مجھے لکتا اس میں اور کچھ یہ بہتی ایذی دیسکسی پیٹھانے ایک برم فٹا فٹ اس کا کلین ایمل دیگرام دیکھتے اور اے اس کے دیسین دیکھتے تو دیکھتے ایک دم سیمپل سا کلین ایمل دیگرام میں ہمارے پاس ہمارے پاس سب سے پر ایک کلاینت جس کے پاس ہے ایک پروٹرٹیپ کرفرینس اسی کو کلون کو وریٹ کر لیا اور آپ کو پتے ایک کلون کیا کرتا ہوگا یہ ریٹن کرتا ہوگا نیو کنکریٹ پروٹرٹیپ اور یہ پاس کرتے دیس دیگرام دیکھتے اب کلاینت کیا سے پروٹرٹیپ سے انتریک کرتے بہت سمپل کلاینت کے پاس ایک زیکیود کیا کرتا ہوگا یہ بجو بھی اس کے پروٹرٹیپ کیا اس کا کلون کول کرتے ہوگا جو بھی پروٹرٹیپ کلون کلون کیا جا سکتے ہمارے کنکریٹ پروٹرٹری اس کو override یعنی کی نہرٹ کر رہے یعنی کی اس کلون پی پی پی پی پی پی پی پی پی پی پی پی پی پی پھر ہو سکتے کاموامجیکز بنانے پڑے پر انوامجیکز کو krayaid karna بہت زاد Expensive ہوگا جہا دیتا بھی سے کنیکشن ایسٹا بھیش کرنا پڑے کامпLEX کنگریٹشن کرنی پڑے یا کنی ایک رکوصت بھیجنی پڑے وگر مگر وگر مگر ایک ان کسیز میں ہم بھیش کرتے ہیں اپنے بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا بھنا
Lesson FAQs
What is Prototype Design Pattern | UML + Code | System Design about?
حن جی تو کرے شاڈ Welcome Back Quarter Army our Celety Series and today we are going to see a new design pattern whose name is Prototype Design Pattern now see this design pattern is very simple but a very specific problem is solved and
What key concepts are covered in this lesson?
The lesson covers prototype, design, pattern, code, system.
What should I learn before Prototype Design Pattern | UML + Code | System Design?
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: prototype, design, pattern, code.
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.
