02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Adapter Design Pattern

The Adapter design pattern enables us to switch logic bases very easily within an application. This is commonly used when we want to simplify the interface usage or when we need to modify our code to fit an interface that already exists.

Video Transcript

00:00Let's get back to creating design patterns and our first
00:03Structural design pattern that we're going to be looking into is the adopter design pattern the adopter design pattern is really used
00:12to basically
00:14Change an interface of an object
00:16So for example you might have an object or a class that is coming from a third party
00:21And you need to plug it into another third party application
00:24But they don't have a way to communicate with each other
00:27And you need to adopt one object to be able to work with another and that's exactly the case when we would use an adopter
00:34This component demands a different interface, right?
00:37So you have your two objects one side has one one side has another they don't know how to communicate with each other
00:42And that's where your adopter comes into the picture to replace
00:47That API or another scenario is if the API is right, but it's missing a method or it has yeah missing a method
00:54And we need that method because without that method the application won't work, so let's jump right into it
00:59And let's see that adopter in action
01:02The next step of our journey is to create an adopter now to create an adopter
01:07I'm just gonna go right inside of our singleton and instead of actually appending items with the ad within the stage
01:14I'm just gonna call it add instead because I want it, but really in the real world scenario
01:19You probably wouldn't change it you would just if you needed to adopt you would just keep the name append
01:23But let's say that we want for a contextual purposes to have an ad and remove because that might be your homework assignment to remove things
01:31Remove elements as well at some given time
01:34Alright, so now that we established that we have an ad and we have a remove
01:37The next step is actually to make sure that we're not sending in jQuery anymore because jQuery does not have an ad method
01:44That would be appropriate for this set stage. So to do that I have to create an adopter at this stage again
01:50This is a theoretical assumption here that we needed to add here and add a much more realistic
01:55Scenarios when you're working with third-party tools and they expect a certain interface to be given but your class doesn't have that interface
02:04So what do you do?
02:05You need that class to remain the same because you're working with it in other elements of your application or even other
02:11Applications you just don't want to change it and that's exactly the scenario where you would use an adopters
02:15Let's go ahead and see how adopters work
02:18Now I'm just going to create this adopter and I'm going to call this a stage adopter
02:23So I'm going to go ahead here and create a function called stage
02:26Adopter now our stage adopter is going to basically accept an ID which will be the ID of the item that it's adding
02:33We're going to have a carry-on of indexes because every time we add an item
02:37We're going to want to know which item it is so we could then remove the item down the line
02:41The next stage is we want to also a context and the context itself is going to be jQuery because we're sticking to jQuery
02:47At this stage. All right. So now that we have our stage and it's hidden behind our stage adopter
02:53It's time for us to take our adopter and add to it the add and remove methods. So let's start with the add
02:59prototype add
03:03Function we're going to add items. How do we add the item? Well, first of all before we add an item
03:09We really want to make sure that that item
03:11Has a unique signature now because we're assuming they're going to be jQuery items
03:16I'm just going to go ahead here and just upgrade my item index or my index of my stage itself
03:21So I know what's the new item that's being added in and I'm going to go to my item and add a class
03:27Again hiding that actual real implementation of jQuery behind the scenes
03:31And I'm going to add and I need a unique signature because an ID alone is not going to be enough
03:36I'm going to add a class to that element. So I'm going to go ahead here and just add one more property
03:41That's going to be belong to our prototype here
03:43And I'm just going to call it SIG for signature and I'm going to set it to be called
03:47Let's say stage item underscore just making sure that it's a uniquely long name
03:52So no one else is going to be adding that name themselves
03:56Instinctively inside of their classes, so I'm going to add that signature and also append to at the current index
04:02Now what I've done here is I've added to the item a unique ID
04:05So I'll be able to fetch it down the line when I'm trying to remove the item
04:09Once I've done that I'm ready to go to my context and append the item inside
04:14So I added here a few steps here
04:16But that added a lot more control and capabilities to my adapter and I'm going to go ahead here
04:21And I'm just going to create one more method and the next method is going to be the remove method
04:26so let's go ahead here and just copy this and
04:29This is our signature remove
04:32function
04:33But needs it's expecting an index to be able to know and and find the item that it wants to remove
04:38And I'm going to go ahead here and just add the logic here, which is a little bit more simple
04:42I'm going to go to my context
04:44I'm going to remove the item and I'm going to remove the item that has a class that has the ID this signature
04:51Plus the actual index that was sent
04:54That's it. I've added the add and I've added the remove
04:57Now that I've created my stage adopter all that's left for me to do is to actually test it out
05:02because now inside of my application inside of my
05:06Inside of my singleton. I'm already using add so that should work now. I'm not going to integrate also remove
05:12I'm going to leave that to you as a homework assignment
05:15But I'm going to go ahead in the area where I'm instantiating my stage
05:18And I'm just going to wrap this around with a new stage adopter now this new stage adopter is going to enable me to have
05:24An add and remove method without the implementation needing to know how it works
05:29So let's go ahead and just make sure that everything is still working and if I go ahead here
05:33And I click around that's still working and if I add elements, they're still working now
05:39Your homework assignment if you choose to do it
05:41And I would love to see it feel free to email me or send me a tweetie at o2 geek so I could see it
05:48But basically what we've done is we've created an adopter
05:50We've hid behind it all the functionalities and functions that we actually were actually doing we
05:55Modify the interface to enable us to actually perform the action that we needed and then we send it into this library that already is
06:02pre-existing and that is an adopter in
06:06This lecture we met the adopter design pattern and in the next one we'll continue with the structural design patterns
06:13Moving into a new one the composite design pattern

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!