System Design Full Course Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD
Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD Transcript and Lesson Notes
حان جی تو کہے استطال so welcome back to the Army In our LLD series آج اس لیکٹر میں حمیک بلکھن نیا دیزائن پیدا ادیپٹر دیزائن پیدا پیدا پیدا پہنے والے یہ بھی ایک بہت اصان دیزائن پیدا آپ کو بہت ایزلی ساما جا جائے گا اور اس کا بھی بہت ایس کے سوطا ہے
Quick Summary
حان جی تو کہے استطال so welcome back to the Army In our LLD series آج اس لیکٹر میں حمیک بلکھن نیا دیزائن پیدا ادیپٹر دیزائن پیدا پیدا پیدا پہنے والے یہ بھی ایک بہت اصان دیزائن پیدا آپ کو بہت ایزلی ساما جا جائے گا اور اس کا بھی بہت ایس کے سوطا ہے
Key Takeaways
- Review the core idea: حان جی تو کہے استطال so welcome back to the Army In our LLD series آج اس لیکٹر میں حمیک بلکھن نیا دیزائن پیدا ادیپٹر دیزائن پیدا پیدا پیدا پہنے والے یہ
- Understand how System Design fits into Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD.
- Understand how Low Level Design fits into Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD.
- Understand how High level Design fits into Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD.
- Understand how System Design for Beginner fits into Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD.
Key Concepts
Full Transcript
حان جی تو کہے استطال so welcome back to the Army In our LLD series آج اس لیکٹر میں حمیک بلکھن نیا دیزائن پیدا ادیپٹر دیزائن پیدا پیدا پیدا پہنے والے یہ بھی ایک بہت اصان دیزائن پیدا آپ کو بہت ایزلی ساما جا جائے گا اور اس کا بھی بہت ایس کے سوطا ہے جب بھی آپ ایک اپلگیشن دوولپ کرتے ہو in fact interviews may be آپ کئی سارے کوئیشنز میں بھی اس کو پٹ کر سکتے ہو تو فتفت دیکھتے ہیں دیپٹر دیزائن پیدا کیا ہے اور اس کو سمجھتے ہیں چاہلو آجوز کرین پے دیکھو سب سے پہلے ادeptر دیزائن پیدا سمجھنے گینا آپ ایسے دیکھو جسے کہ ریل life adaptor اوتا ہے فور اگسامپل آپ ایک ہوتیل رومے گئے جاہپے US ٹائپ سوکٹ سے ٹیک ہے لیکن آپ کے بات جو چارجہ رہنا وہ ایک Indian socket based so آپ کیا کروگے آپ ایک international adaptor use کروگے جو اپ کے Indian charge کو اس میں pluck کر پائے گا اور کھت پلگ ہو جائے گا ایک US socket kender right تو یہ کامتے ریل life adaptor کا اور دوسر ایک بلہ پیدا پیدا تک سکتے لیکن آپ کے پاس ایک ٹائپ C كیبل ایک تو آپ ایک ٹائپ C سے charge جو سکتے پر ہمار پاس جو adaptor ٹیک ہے وہ ٹائپ B لیتا ہے تو اگئے آپ کو ایک adaptor use کرنا پڑے گا ٹائپ ٹائپ C کو ٹائپ A ایک انا نوضید کرے گا ٹیک ہے تو یہ سب ریل life use ke adaptor ske اسی درا سے بلکل same طریق سے نام جو ہم programing make adaptor ke baat کرتے وہ ہم یہ help کرتے ki let's say ہمار پاس ایک interface let's say A اور ایک دوسر interface let's say بی اور ایک completely different interfaces اب جب برائی سے interface میرا کیا مطلبостکتے ہیں ایدر یہ ایک контакٹ ہو سکتے ہیں یا ایک such may abstract class سکتے ہیں جاوہم اچھے سے مinterface کہتے ہیں کچھ بھی پڑ دو clases یا دو abstract clases یا دو interfaces چو completely different ایک دوسرے سے اور جو normale آپس میں بات نہیں کر سکتے ہیں ہم ان کے بیچ میں ایک adaptor کو انٹروڈیوز کرتے ہیں اور وہ adaptor ان دونوں کے بیچ میں بات کرنے میں بات کرنے کے بیچ میں اوہم ابھی دیکھیں اور ای ہو آپ ایک اوووریو دو لگیا ہو گا کہ adaptor pattern کر کیا رہا ہے تو اس کو ایک ایک اگ ببا؟ سے انت رہا لیکن اس سے پہلے دیکھتے ہیں کہ جنرلی adaptor pattern آیا کیا سے تو دیکھو ایسے ہوتا ہے کی آپ کا جب آپ کوٹ لکتے ہو تو آپ کے پاس کوچھ ایک existring کوٹھ ہوتا ہے یا اپیکشن کا کوٹھ الیڈس یا اپیکشن ہے تو جب آپ اپیکشن تو آپ کو کئی بار, ایسے 3rd party applications یا 3rd party libraries ki 3rd party classes ki zharurat pati hai, جو آپ اپنی application intégrate karta hai, آپ ہر چیز اپنی application na khuttao ni kareoge, raih, hart cheese kudimplement karene karene kagosis nao nahi kareoge ttao, ttao, ttao, ttao, ttao, led se he hai, اپنی ایک 3rd party library, ہم اس کو یہاں لکتے ہیں, 3rd party library, тکے, تو generally, کیا ہوتا ہے na, یہ جو آپ کی 3rd party library hai, اس میں alat classes lekihoongi, alat tarike se methods hoongi, raih, bülkul different hoogi, آپ ke existing code se, اور کئی بار, یا تو آپ 2 کام کر سکتےo, یا تو آپ اپنی ہی code میں پر اس سے آپ نے کیا کیا, اس سے آپ نے اپنی existing code ko, taitli kapal kardhia 3rd party se, یا نکی جب بھی مرگے 3rd party library badaal doongi, فرگسیم پل میں notification system ke livery use kardhau, ابھی میں ایک library use kardhau, ایک کل کو میں نے library b bezhift kardhia, کیا, کیا لیٹ سے وہ میر کو sastii pardhia, cos deficient hai, raih, زاادہ بیٹھر ہے, تو میں library b bezhift kardhia, تو مجھے کہاں جنگس کرنے پڑے گے, اپنے existing code میں which is wrong, تو میں کیا کرتا, میں ایک adapter کو introduce kardhau, تمہی یہاں سے سے call na kardhia, بیچ میں ایک adapter کو introduce kardhau, لیٹ سے وہ ہے یہ, یہ مرہ adapter ہے, اور اس adapter کا کام کیا ہوگا, اس adapter کا کام وہی ہوگا, جو ایک real life adapter ko, کہ یہ ہمارے existing code سے بات کرے گا, اور یہ adapter our third party library se, ابھی بات کرے گا, اب ہمارے existing code کو نہیں پتا, کیاں کون سے third party library use kardhia, انگسیسٹین code کو بس پتا, کہ میں ایدапٹر کو call kardhau, اور ایدапٹر ہمیں کچھ return la ke deka, باکی adapter focus kardhau, third party library se, بات کرنے گے, یہ ایک بہت اچھا یو اس کے سے, تو اب ہمیں سے ایک example کے through s, آپ کو زیادہ بیٹر چیزے کلیر ہو پائیں گے, اس سے پہلے ایک بار اس کو دوارہ سے کلیر کر لیکن کہ ہمار existing code, ہمارے پتا, third party library, اس سے انتریکٹ کرنے گے, ہمیں adapter کو introduce kardhau, کیاں کچھ introduce kardhau, ہو سکتا ہمارے جو existing code, وہ بہت ایلکتری کے سے لکھا ہو, third party library بہت ایلکتری کے سے لکھا ہو, سکتا language different, یہاں پہر جو وہ return, وہ بہت ایلکتری کے سے لکھا ہو, یہاں پہلے جو وہ ہمیں ایک جیسٹن کوڑ میں ہم اس کو tightly couple, اس ریزن سے ہمیں adapter introduce kardhau, جو کی بلکھو لیکھو لیکھو لیکھو لیکھو لیکھو لیکھو ایک جیسٹن کوڑ سے, اور ہمارے پتا, یہاں پتا, ہمارے پتا, اس سے انتریکٹ کرنے گے, اس سے انتریکٹ کرنے گے, اس سے انتریکٹ کرنے گے, تو آپ پزل پیسے سے مجھن کرو, کہ لیکھ سے یہ ہمارا ایک جیسٹن کوڑ ہے, this is our existing code, and this is our third party library, so our existing code, our third party library and both the puzzle pieces are not joined in the joint. So, what do we do? We introduce an adapter which from this side, we take our existing code, and this side of the puzzle piece we take our third party library, and what is this, our adapter. Now, you can see these three puzzle pieces easily joined.
So, this is the design pattern of the same work. Now, this diagram, you will get better clarity here. Now, jump like your example, and see all the standard UML diagrams and standard UMLs. So, this example, let's suppose, we have a client.
And what does this client do? We have an application which we are going to fetch reports in which in a format, let's suppose this client wants some reports in JSON format. So, we expect a client. We expect a client that we have a class, let's say, reports, reports.
And now, this can be done by our interface, we use the same nomenclature, i-reports. So, we have a i-reports class. So, we have a method, get JSON data. And because this interface, we will not define it here.
Rather, we will declare it on the top of the class. So, what does this client do? It will interact with this interface, and then, get JSON data, and get JSON data. This is simple work.
So, the work we have in this software, is get JSON data to implement. So, this client will expect that we have got JSON data, and report. Now, let's say, we can pass a string here, and make JSON data. And then, the third party library exists, which is given by an example data of any report.
Now, our work will be done, and we will convert the example data to JSON. So, what we will do in that case, let's suppose, this is our third party library. Okay? What we do, let's say, the exam data provider.
This is the library's name. Okay? So, you will understand the name. This library, what we will do in that case, we will add the example data to it.
Okay? So, we will have a method, let's say, get the example data. And now, this class, this can be an abstract class, or an interface, which can be given by some concrete class. This can be given by a single layer.
So, for simplicity, we will take this class, and then, this is the concrete class, and then, this is the example data, we need JSON data, and we want to convert it to this library. So, in that case, what we will do now? We will introduce an adapter, what we will do, we will adapt it, we will adapt the example data provider. And we will take JSON data and take this example data.
So, now, look at these two different interfaces that we cannot talk about in the office. We have seen the puzzle example here. This is our existing code, and this is the third party library, so, we will introduce an adapter. So, we will introduce an adapter, we will make a class, which is called XMLDataProviderAdapter.
Okay? So, we have made an adapter for XMLDataProviderAdapter class. Now, what will be the work of this class? This class will be the work of this class, that this is the report.
Okay? This gets JSON data to override it, because the client is expecting JSON data. So, first of all, what we will do? This is the XMLDataProvider, it will override it, or it will actually inherit it, it is a relationship.
So, our adapter is a report. So, this is the first thing. Now, if it is going to override it, it will have the same method to get JSON data. Okay?
And we will define this in this, we will take JSON data, now, how will we take it? We will take this and use the XMLDataProvider. So, this will have a reference of the XMLDataProvider, that is, it has a XMLData. So, the adapter is the user report, and it has a XMLDataProvider.
So, we take its reference, so, here, this is our XMLDataProvider. Let's say, pro, we will name it, this is its reference. Now, generally, what will happen? Whenever a client has a reference of a report, the right will have a reference of a client's report.
I, a report, will have a reference of a report. Let's report. Now, in this report's reference, we can also pass something, which will be an I report type object. So, in this I report, in this report, the XMLDataProvider adapter can also pass.
That is, a reference of an adapter has come to the client. Now, just define this reference report. I will just get JSON data, and I will get JSON data. Now, as soon as this report gets JSON data, it will be called, that is, this function will be called, get JSON data.
Now, actually, what will happen? If you think about its implementation, then what will happen in its implementation? First of all, this reference has a reference of the XMLDataProvider, pro, this will be called, first, get XML data. Right?
So, this XMLData will be found. And then, this XMLData will convert this, into JSON data. And it will be written, and our client got the JSON data. So, what did the adapter do here?
Basically, it is a behavior, like a report is a behavior. Like, if it is a behavior, then it is a relationship with it. And to behave, it has overrided the same method, it has overrided the JSON data. That is, there is no client, who is expecting the I report, we can pass this adapter, it is clear first.
Now, when the client will call the JSON data, then what will the adapter do? This, with the has a relationship, with the adapter, if it is an adapter, then the one who is adapting, like now, we are adapting this XMLDataProvider, then we are talking about the adapter, meaning, the adapter is adapting. So, the one who is adapting, first, get XML data, call the method, and then get the XML data, which we have found, and convert it to JSON data. And it has written, the simple, what did it actually do?
Two different interfaces, which we do not talk about, we talked about it in the past, using an adapter. So, now I hope you understand this diagram, better than I think, that actually, we are adapting the adapter design pattern. Okay? So, once we see its definition, okay?
You will see more clearly, and then we will see the whole thing of the whole thing. Okay? So, look, this is its standard definition, adapter, converts the interface of a class into another interface, that client expect. So, we did the same thing, which we were expecting, we had an interface, which was a third-party library, which was an XML data provider.
We converted it, using XML data provider, adapter. Adapter led classes work together, that couldn't otherwise, because of incompatible interfaces, which are very simple. And an adapter actually, what is the work? So, we have to interact with such interfaces, or with such classes, which we do not interact with, which we do not interact with, because we have different interfaces, and they have different methods.
They work on different things. For example, we had an XML data provider, which we had an XML data, but we had to have JSON data. So, we have introduced two different interfaces, so we introduced the adapter, and then we talked about it, that simple. Right?
So, now we see the code, for the effort, so this is the whole thing, the code we talked about, is exactly the same, okay? First of all, we have created a I-report name, which was the interface, which we have written, which we have written, and now, what we have done here, is that, see, we have written a raw string, in its parameters, which we will write, and then we will write, okay? Now, we will make our own adapter. Here, our adapter is called, our third party library, third party class, which we had given the XML data, so this is our XML data provider, which we will write, and then we will write, and then we will write, now, see, what we have done, and then we have made a raw string data, and we have made some XML data.
We have done the string data, which is the format, name, colon, id, and we will fetch it, and then, its XML data. Now, this can be something, okay? This is the specific use, now, we have just implemented it, for example, we have implemented it, we can implement it in any way, depending upon, the raw data is, how it is coming. Now, we have decided, that our raw data is in string, but in real life, you can see, that the user interface, the reports, which you convert in XML, right?
or PDF formative data, or let's say, comma separated values, or Excel sheet data, without saying, that example can be something, but, for example, we have just done the string data, and converted it into an exam. Okay? Now, we have made our own adapter, which is called XML data provider adapter, which we have made. Now, we have said, this will inherit, we will do our report, this will inherit, that is, it is a report, first of all, and we have a reference, our third-party library, that is, XML data provider, that is, the adapter has a provider, right?
So, here, we have taken a reference, in its constructor, it has a reference, and this reference, is declared here. Okay? Now, we have the method, which will get JSON data, it will override this method, it will get JSON data, okay? It has override it.
First, what will we do? This will take our raw string data, first, it will get the XML data from the adapter, so, the XML provider, it has called the XML data, and we have got an XML data. Now, we will try to convert the XML data into JSON data, so, again, we have tried to convert this example, the example can vary, so, what we have done here, we have found the name text, and we have found the same ID text, and then, we have added a JSON format, and then we added it to the JSON format. Simple, okay?
In any way, you can implement this, this is implementation specific, or business logic specific, but, we are now understanding what is happening. Okay? Now, we have made our client class, in the client class, we have a 1 method guide report, which is a report, and the raw data is, and it says, we call it 1 method on this report, get JSON data, okay? So, we have this, that we have a report, which we have to call it as a get JSON data, and we have to pass the raw data, and we will get a JSON data.
We don't have any client, we have to use the third party library, or we have to implement it, we don't have any client. We don't have any client, so, we have to call this method, and we have a JSON data, we have a simple, okay? Now, what we have done here? First, we have made an Adaptive, XML data provider, then we have adapted, I have adapted, I have adapted, which is the type of XML data provider, and what we have given it to the reference, we will adapt it, then we have made an Extreme, at least 42, so, this is our name, and we have made an idea of it, okay?
Now, we have made a client, first of all, and the client called get report using this adapter. So, the client was expecting here, the client was expecting here, a report type, right? And we have passed it to the adapter type, because the adapter is also a report, so, we have to inherit it, right? We have passed an adapter, and we have given it to the raw data.
Now, as we have called the method, the method of the adapter will be called, it will be called as the adapter, it will be XML data, and XML data will be converted to JSON. So, now, if we run this code, and see, so, see, we have got a name, at least ID 42, okay? I hope you have understood the adapter pattern clearly, now, once we see the standard UML diagram, so, look, this is the standard UML diagram, the adapter pattern, what will happen to us, the interface, the target interface, and the second interface, the adapter interface, okay? This is our target interface, this adapter interface cannot be talked directly, because, there is a method request, but, there is a method, a specific request, what we have done, we have introduced an adapter, what we do with the adapter is a report, and how we adapt it, okay?
And, what we do in this request is, we call the specific request method, this is our report example, and what is the client doing, the client is just talking about the target, so, this is our standard UML diagram, I hope you have understood better, so, now, before discussing the use case, we have used the adapter pattern, where we use the real-world applications, we have to talk about a small thing, that, now, we have talked about the adapter pattern, we have talked about the standard UML diagram, this is the object adapters, okay? Now, you ask, what is the object adapter? Look, what is actually doing? This is an object adapting, in this way, okay?
We have another kind of adapter, which we call as class adapter, there is no difference between the two, we use a composition, class adapter will use inheritance, how? The diagram will change a little, that we have, one of our target, i target, okay? and one of our target adapters, we use our adapter class, so, this is our adapter, okay? Now, the adapter, okay?
So, the adapter, what was it doing? It was inheriting the target, and, it was able to combine the composition and the relationship, with the adapter, we have changed it a little, what we have done? So, we will use the inheritance relationship, okay? with the target, with the adapter, that is, we will use this multiple inheritance, and we have told this to the class adapter, and there is no difference, we will also do the same, we will have a request method, and we will have a specific request, what will be the work, we will override this request, and we will call this specific request, we will call it as class adapter, and we will call it as object adapter, and we will use this inheritance, and we will use a composition, so, this is the same thing, the first debate, inheritance, inheritance, composition, the second debate, we will always refer to the composition, over inheritance, in real world applications, so, generally, one of the first, is the first, the first, the second, if you come to C++, then, in real world applications, we do not prefer that multiple inheritance, in fact, we always say, prefer composition over inheritance, so, in multiple inheritance, we will not even talk about it, so, for your knowledge, I have told this to the class adapter, and a request adapter, sorry, and an object adapter, but we always prefer object adapter, or a composition relationship, ok, now we will jump, in real world applications, we will also understand, that whenever you, you have to introduce your project, to a third party vendor, or to contact, or to contact, then, you introduce an adapter, which we have an example, which you have an existing code, and you have your vendor code, which you will introduce in the middle of the adapter, which you will inherit, and with that composition relationship, and it will be able to talk about both of these, so, whenever, in your code, you will use the third party library, to integrate, use, then, you will understand, the adapter design pattern will be used, what can happen in the second use case?
So, in the second use case, think that, whenever you develop an application, you have to work with a legacy code, what is a legacy code? a legacy code means, a lot of old codes, you can say, but a legacy code, which is used before 8th, what will happen in that? some methods, which are deprecated, some methods, which are not used, so, your modern application, and this is your legacy code, because you have to talk about a legacy code, because this application is already written in the old Java, or C++, you can change it, it is not your application, so, what will you do again? you will introduce an adapter, that adapter will work, take a code from your modern application, and call the methods of a legacy code, then, contact the legacy code, and contact the same thing, which we used in the third party library, in the same way, we will also use this, so, these are some real world use cases, as we will see some examples, you will see some real world use cases, you will see some real world use cases, I hope you understood the depth of the app, I will see you in the next video, till then, thank you so much.
Lesson FAQs
What is Adapter Design Pattern | Real-world use case + Code | Design patterns in LLD about?
حان جی تو کہے استطال so welcome back to the Army In our LLD series آج اس لیکٹر میں حمیک بلکھن نیا دیزائن پیدا ادیپٹر دیزائن پیدا پیدا پیدا پہنے والے یہ بھی ایک بہت اصان دیزائن پیدا آپ کو بہت ایزلی ساما جا جائے
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 Adapter Design Pattern | 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.
