The Adapter Pattern (Design Patterns in C#)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right this is Steve I'm gonna make a video here or try to to give a good example of the adapter pattern that's one of the 20 some patterns defined by the Gang of Four and the classic design patterns book I'm gonna do this in c-sharp and I'm gonna try to write something that that seems a little more pragmatic and explain it along the way so alright we'll start an instance of videos visual studio here and once again I'll be coding this in c-sharp and what I'll do is I will go ahead and start a new project and I'll go ahead and make this a console app and doesn't really matter if it's dotnet car or regular or unleaded it's just a pattern it's pretty basic on a c-sharp and I'll go ahead and call this okay so I've created this project here it's called demo adapter pattern and I'm just gonna make this a console application and the idea is we're gonna think of Maine as our client or or actually we'll just create a little class here execute go ahead and control period here and we will generate a method called execute client and the idea here is that in this case a client is the code that calls other code so sometimes that client can be a user interface or an API or something at the top tier but it really it's relative the client will be calling other code instances since it's a console app I'm gonna go ahead and put a little wait for user read key just so we can see some results all right and throw a little go ahead and execute this just to make sure that we can see that our application is running and I'll drag the console app onto the screen here you see Hello adapter so we are good to go we have a client now I'm gonna go ahead and what I'd like to do in this example is I'm gonna use a classic example of a bag of fruit and what I'd like to do is I'm kind of this mindless drone who puts a bunch of different fruit into his bag and then at the end of the day I'd like to go through that those items in my bag of fruit and pull one out at a time and then peel it now the problem here is not all fruit is peelable an example would be an orange is peelable a banana is peelable but an apple not so much that'd be kind of hard you'd have to dig in with your fingernails so you'd probably you probably end up o skinning it so an apple would be skinnable and a pair would be skinnable so I'm gonna go ahead and I'd like to create a list of peelable things so for background an adaptor you've seen it before it might be you might be traveling going on vacation maybe you're from the United States and you go to Canada and you have your alarm clock and you'd like to plug it into the wall and maybe for some reason the hotel doesn't use a United States standard outlet for your plug so you might get yourself a United States standard two Canadian standard adapter you would plug your your alarm clock into the adapter and the out the adapter would be plugged into the wall the idea in code with an adapter is that you might have your client might be expecting a collection of objects that behaves to a certain kind of interface but you might get a collection of objects that is different than that interface so an adapter would basically translate one interface to another all right so what I'll do is I'll start by defining a bag of fruit and I like to code first and kind of code down and create the things as I need them right so I'm gonna create a bag of peelable fruit because remember my intention is to go through this bag of fruit and peel all the things you list peelable things and when I'm using an interface I like to start the standard is we start with the letter I so in this case it's AI peelable all right so we've got a couple problems right here the first thing is my code doesn't know what a list is so me a control period and it's gonna give me the option to correct that by using system collections generic so this is just a way to hold on to a collection of things I could have used an array or I could have used a dictionary or anything that that holds on to a group of objects in this case I enjoy working with lists so I'm gonna use a list the second issue is I have I'm referencing an interface that doesn't exist yet I haven't created it so I'm going to go into my program and I'm going to create a folder and I'm gonna put my things into a folder called models models is really just a fancy word for things things that you work with kind of like business objects but usually meant to hold data sometimes those objects can do things in this case they can be peeled so I'm going to go ahead and add a an interface right now I have a class that is peelable and my class is gonna be or my interface is gonna be really simple it's gonna do one thing it's gonna have a method that peels and that's it don't even need these guys so I'm gonna do a quick remove in sort and I need to make this an interface there we go cool all right now normally I would create my concrete classes first and extract an interface but now that I have created peelable it can give me the option to add it as a reference so now you see I've got demo adapter pattern models is being referenced because it's inside my namespace of models I peelable is in camo adapter pattern models alright cool now that I've got this list of peelable things I would like to add some some things to that list so first thing I want to do is I'm gonna go and I want to go ahead and take my bag healable fruit now and orange that we got a problem here this orange doesn't exist so quick little refactoring I'm going to go ahead generate an orange class in a new file cool created one here I'm gonna slide that into my models folder once it's in my models folder I'm gonna go ahead and now here's the really neat thing because I used that shortcut by right-clicking and refactoring it knew that I was trying to add orange to a list of IP labels so it automatically implemented the IP level interface well it didn't implement it it it added it now it's gonna ask me if I want to implement it so go ahead and click implement and wham-o there's my method now I have an orange that is peelable because it has the peel method so instead of throwing an exception I'm gonna go ahead and just do a console right and of course I need to add cons new system and we're gonna do a console line and let's say eel and orange cool I'm gonna save my work and I'm gonna go ahead do the same thing I take my peelable bag of peelable fruit and I'm gonna add a new banana and once again I'm going to I could just go to this little light bulb that appears automatically or I could hit control period and I like this option it's gonna say generate class banana in a new file so I'll go ahead and click that now banana is down here and notice that go ahead and now the neat thing is is it is using models because it's I peelable so what I'm gonna do is I'm gonna clean that up a little bit I'm gonna throw banana into my models folder and I need to make a little change now because my bananas is in the models folder I should change my name space to demo adapter pattern models and then I don't need this using statement anymore and in fact I should do the same thing with all my other so I'm gonna take this model change my name space don't need that I'm gonna save my work and open up a banana you'll notice that I've got that little warning that says hey you haven't implemented peelable so we'll go ahead and click implement interface once again it creates the method it says execute the peel method or gives the user the ability to execute the peel method and once again I'm just going to use a console.writeline notice the console doesn't exist so Mattie using system and I will say killing alright for Fonzie's I'm going to compile make sure everything builds build a succeeded so I'm gonna go back to my program and now I have a bag that includes an orange and a banana now before I implement an adaptor at this point we haven't done anything with an adaptor all we've done is we've created a bag of peelable fruit and what we'd like to do is we want to go ahead and loop through the items in our bag peel all the fruit our bag and such a little for each tab tab and we'll go ahead and bag of peelable fruit back so we are going to call it a fruit as each item as a fruit well I like just calling it fruit but since I really have a list of I peelable my intellisense was smarter than me and it was saying yeah you really want to call this peelable for for our conversation here I'm gonna go ahead and call it a fruit but there right it is a peelable item because you could have oh you know other kinds of food that are peelable all right so now that I'm iterating through each of my fruits within my bag of peelable fruits I'm gonna go ahead and execute boot dot peel build that code and I am going to run it in my console and I'm gonna drag that console on to the screen so you can see it it has now iterated through my list peel an orange peel a banana as a reminder when you go to the banana class you see all our do all I'm doing is writing a line to the console when I hit the peel I'm just saying peel the banana all right so now here's where it gets interesting I'd like to I'm gonna work a little little differently here then I'm gonna go to my models folder and I'm gonna go ahead and add a class and I'm gonna create I'm gonna create an apple and this Apple is going to have a method called skin so I'll just lick lead in the reminder when you're using void that's an action as opposed to an expression nothing is being returned so the action is to skin this Apple and I'm gonna go ahead and console.writeline just remove those unused sorting x' and go back to my main program and I'm going to add and here's the thing you think's gonna happen it's gonna say you can't add that because it's not peelable right let's look at that Apple should we make it peelable should I here if I do that doesn't work because this Apple is not it doesn't peel its skins so I'm gonna undo that work and in the real world the reason we use adapters is because this Apple probably already exists and in the client the client is expecting a collection of peelable fruits and we already have some existing code that contains skinnable fruit so I'm gonna go ahead and create an interface I'll right click on this use quick action and refactoring and I will extract an interface we'll call this a bowl and I'd like it to go into a separate file and just to check my work I'm gonna open up I skinnable cool and it's got my interface the contract that says to be skinnable you have to have a method that says skim and our friend refactoring tool went ahead and created the file for us now apples are skinnable what else is skinnable let's go ahead and add another class let's create a pair cool all right remember air is skinnable gonna add a little it's gonna warn me it's gonna say hey so I'm gonna hit control period to get that nice little refactoring menu to show up and I'll go ahead and implement the interface it's gonna create an unlimited method for skinny and I'll go ahead and implement it by writing to the console and I'll go ahead and write in a pair once again I'm gonna clean up my unused usings well now I've got a pair I've got an apple I've got a banana and I've got an orange but the difference between my but my orange and my banana versus my Apple and my pair is oranges are peelable they implement the peelable interface and you can see there's the peel method for them versus an apple or a pear that has the skin interface so I'm gonna go back to my program once again I'm gonna try to add this Apple and I also want to go ahead and add and once again you can see the pair also doesn't work because it's warning me that I can't convert this pair to appealable so how would we do that this is where the adapter comes in so I would like an adapter that converts skinnable things to peelable things all right so I like to write my code before I have it gives me an idea of how I'd like to consume it in this case I'd like to create a new make sure I spell that right skinnable to healable after and I like to say what it is so at the end of my class name I like to say if it's an adapter I'd like to call it an adapter all right skinnable to peelable adapter all right so it's gonna say it doesn't exist all right so we're gonna create this into a new file cool now I've got this class I like to put my adapters into a separate folder is to stay organized so I'll go ahead and create this folder and I'll call it adapters and I'll go ahead and put this guy into my adaptors folder click on it to open it and update my namespace because you want our name spacious spaces should always match our file structure because that makes it easier to find our code and alright so one thing you'll notice is that because we used the refactoring tool from when we were coding from our program file when we auto-generated that adapter it knew that I wanted it to be healable so it needs to implement a peelable interface right so I'll go ahead and click implement alright that's neat also what's kind of cool here is that it implemented the dependency injection for me has said hey you're gonna want to you're want to convert the Apple so except the thing is I'd like to convert more than just the Apple I want to convert the apple and the pear and pretty much anything that's skinnable so I'm gonna change this from Apple to PI skinnable and I'm gonna change that here to my backing variable that it also created for me and it's not really an apple anymore so I'm gonna go ahead and rename that to in a bowl and my backing variable I'm is also skinnable I like to capitalize my properties depends on where you work and what your standards are some people like to put underscore in front or keep their private variables a lowercase I like to keep all my properties to use Pascal case and because later on I might refactor this and make this public or or whatever and you know if I ever refactor a property I don't have to change the name so that's not really important as much as what is important is you're using dependency injection to inject a skinnable thing you're gonna set it to a backing property and then when you execute the peel on it we're gonna go ahead and peel that thing so in this case I'm gonna take the skinnable backing property I'm gonna go ahead and seal it skin it what I did there is I've got this adapter and if I execute peel on this adapter it's gonna translate that action to the skinning on the object that was passed in go ahead and save that go back to my program and it makes more sense if you look at it from here I've got this adapter now I just need to add the reference Oh wrong hotkey Corian I'm gonna go add using I've got this nice little all right so that should work so I've got this adapter and I'm adapting this Apple to an interface that expects I peelable so you notice that the pair is not working at is still complaining so I'll go ahead and also throw it into the adapter cool my compiler seems happy I don't have any red squigglies so I'm gonna go ahead and run this go ahead and track my output window and to this screen and you can see peel and orange peel a banana skin and apple skin a pear so there you go hopefully that's a more tangible example of using an adapter now in the real world what's probably gonna happen is you're gonna make a request from your client and the client maybe is expecting a certain kind of business object or business model it has a certain kind of interface that lets say works for the present presentation tier and maybe you're gonna bind that to the user interface but your be held into a certain kind of interface now you might have a very similar object maybe at the business logic layer or in coming from the service layer or whatever but it's just a little bit different and maybe because of coupling reasons or maybe because it's an external library and you don't have control over it you're not really able to change that the source interface in this case the source interface would be the skinnable so you can make that skinnable object play well with the peelable expectations by using an adapter all right I hope that helps
Info
Channel: Imposter Syndrome
Views: 959
Rating: undefined out of 5
Keywords: design patterns, adapter pattern, visual studio, c#, code sample, live coding, interfaces, object oriented programming, console
Id: W-JjO1A0PrU
Channel Id: undefined
Length: 25min 29sec (1529 seconds)
Published: Tue Feb 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.