The Adapter Pattern

A free video tutorial from Chiranjivee Thakur
Software Engineer
Rating: 4.5 out of 5Instructor rating
4 courses
21,112 students
The Adapter Pattern

Lecture description

Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

Learn more from the full course

Learn Structural Design Patterns in Java

The only course you need to learn structural design patterns!

01:35:47 of on-demand video • Updated November 2018

Think about the design choices in a better way
Be able to code a efficient and cleaner solutions for commonly known problems
Be confident at understanding and explaining Structural Design Patterns.
Be able to come up with a well thought design for the problem in hand
Be able to communicate well in design discussions and convey ideas fluently within team.
English [Auto]
Well Hello everyone my name is Churi and agree and in this video series you will be learning the structural design patterns so structural design patterns are basically responsible for building simple and efficient class hierarchies and relationships between different classes. So there are quite a few design patterns in this category and we will be discussing adapter bridge composite Decorator Facade lightweight and proxy. We go through all these structural design patterns one by one explaining the problem it is trying to solve and the UML diagram and we'll also go through a coding Zambal which will explain a real time scenario where we can apply this design pattern. So starting off with that after about let's take a look at what is adaptive about and what the problem is trying to solve. Although they go UML diagram offered acrobat and as discussed and we'll take a look at the recordings up. So let's get started. Well Raptr is a structural design pattern that allows objects with incompatible interfaces to collaborate. What does this mean. What does this incompatible interface to cooperate mean. This is the main essence of the adapter design better so what. And Apple padron mythically allows is it allows some interfaces which are not incompatible retrieve want to use adapted by using adapter badan we can reuse those interfaces without writing ex-drunk or. So the way this design pattern works is it can work the interface of a class into another interface which client is expecting it all tallents classes work together that couldn't work otherwise because of that in compatibility of the interface. Let me try to visualize the problem this adapter pattern is trying to solve. Let's imagine a scenario where you have an application in which the program is talking to a file system and the way that interface behaves it exposes methods to read from the file system and write to the file system. So let's imagine a scenario where you want to support one more file system which also behaves in a similar way. But the interface it is providing is not equal or compatible with the interface the client is already using. So let's look at the UML diagram first and then we will try to see what is going on behind the scenes. So this is the UML diagram for the adapter pattern. So as you see you have a line which is always targeting an object which is of type target and there is a request method in that object which client is accessing. So let's say you have one more class which has also a similar kind of behavior but not the interface. So that means that the interface that is providing is different from the interface the client is already expecting. In this case which is a request and the interface which are expected to last providing is a specific request. So you see there is this kind of semantic discord client called Can we use to work with the near ADAP the object which we have in this problem. So the way to solve this problem is to add an adapter which implements that interface which client is already targeting do and have a reference to adapt the which this adapter will adapt to. So what this adapter will do is take the client request and convert it to the request which this adapter supports. So let's say our late stage our fi system example lets say your client is writing to NTFS system which is providing relief from the system and right to the system as do interfaces. So this client only knows two things. Read from the system and write the system and it does not know anything what is going on behind the scenes. Let's say you are injecting NTFS file system which is implementing those semantics. Now you want to extend the functionality and you want to add support for lets say e XTi file system which is basically used in Linux. But the problem here is that those file system does not have these method letted those findings them have completely different sets of interfaces for reading and writing to those systems. So what we can do there is Indic the adapter at which we implement the interface the timecode is using and give it and have a reference to the newer adapter Econ class. In this case our linux file system and BVA just delegate the call from the client to the new or specific method of that particular file system. So the way to implement this was to just take a look in Java and we tried this. So let's take a look at the good example here. I have created a very small example would be more straight. The working of adapter pattern. So what we are doing in this example is we are simulating some birds. So here are a plain glass or plate glass which is referencing a target interface. So here in our case the Dalgard interface is about interface and the only thing this interface does is still makes up. So any class which implemented this interface would have to give a special behavior for this mix of method. So I have created one number which is called up as you can see this Watrous So the makes on whether it is implemented by this document class. And let's go to a blank as can be and as you can see we are instantiating this doc and we are depending on the interface to actually have the functionality of this SQLite not make something with it. Now let's say your requirements change and you want to also include blasting birds or plastic toys or whatever makes sound in your simulator so you create how you go and create a class because last October. Let's say for I don't have this make sound method there in that interface. And instead what they do is you squeak. So when will you be there to squeeze some plastic toys. This is the sound you make. So aerobatic Diebold or whatever can have their own implementation of their song. So I have one more class which is called the Penguin which implements the plastic Deibert and the implementation is it is squeaking. So again this is squeaking out and this guard it squeaks but how can we use dysplastic to interface with our board interface. Because she's our plainclothes call is strictly dependent on our interface. It is tightly coupled to the method that is calling so to solve this problem. What we saw in the human diagram is we need to create an acting class which we implement that target class and how or if it should end up the cookie go in our core so to demonstrate this I have already created a boarded up. And since I'm getting the bot interface we are implementing the book let me know. OK. So we haven't really written the book interviews and after implementing the interface what we need to do is we need to overate the make some other kindergartener's. So that's what we did. Now the second point is we need to have a reference to an adoptee. So in this case our adoptee is over. Our board is adapting to our board interface so we have refreshed our property and to instantiate this adoptee and create the taking a reference in the constructor and setting it here. So let's go to our place called and see how we can implement this. So clangor when we instantiate the word object what we do now is again the word which would be a bad word and to instantiate it with that Abbotabad and what we do is to instantiate an instance of the byte adapter. And here we pass a plastic toy but so the implementation of plastic toy but isn't so this need up to us so we passed up in class and close that night. Now if you actually do Guibor don't make some and actually run this you'll see that squeaking arquebuse there which means that whatever was there in the movie The Penguin which implemented the plastic timer interface which was no event equal or equivalent to our interface. Now we can use that. So this is how we implement the adapter pattern. It's a structural design pattern which lets you use those interfaces which don't have compatible X-post metrics. So we saw how our client is using that argument and how we can actually create one more adapter using our target and have a reference or an IP and actually delegate the call from that Dalgard to the adoptee and get the output. So thank you guys for watching my video. And in the next video we discussed a bridge pattern. So see your excellent.