Bridge Design Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well no internet and welcome to part 15 of my design patterns video tutorial today I'm going to talk about the bridge design pattern so what is the bridge design pattern well the official definition is 2d couple an abstraction from its implementation so the two can vary independently yes that is a bunch of gobbledygook and I have found through numerous different tutorials that the bridge pattern is often very poorly explained or glossed over completely and on top of that everyone seems to explain it differently in this part of the presentation I'm going to explain it the way that I have always used the bridge design pattern and the way that it was explained to me by someone much more intelligent than I and my basic definition is to progressively add functionality while separating out major differences using abstract classes so let's go into a bunch of pictures and explain this further so let's say that you want to come in here and you want to ultimately create remote controls that are going to work differently depending upon very specific televisions and along the way you want to define all of those attributes that each television or device will have and then remote controls along with that so if you're going to be creating the most abstract of televisions what are - it's a functionality that every single device ever is going to have actually in fact whether it's a television or a radio or a DVD or so on each one of them is going to have a remote control that is going to control volume so what we're going to define here in our first abstract class are the buttons that are going to define volume for all of our devices seven is going to represent up volume and eight is going to represent down volume then afterwards we're going to create more concrete devices and this first television which we're really going to focus on is going to define what five and six are going to do which means they're going to define that the channel number should increase wave number six is pressed or decrease whenever five is pressed however let's say TV two is actually a DVD player in that situation button six will skip to the next chapter well button five will skip to the last chapter then we come to our abstract remote control what it is going to hold is a reference to a specific ice either the basic TV or the TV that is going to have a DVD player connected to it it is then going to find all those abstract methods that are going to differ between this remote right here and this remote right here and there's very specific differences are going to lie with button 9 remote 1 whenever the button 9 is pressed is going to mute the sound while down here with remote 2 that we're going to define whatever it is pressed let's just say it's going to be connected to a DVR which is going to pause the television so along the lines we're adding different layers of functionality with an abstract class that lies between hence 2 layers of abstraction between classes and one class that is going to be dependent upon the other so wind should use the bridge design pattern when you want to be able to change both the abstractions or the abstract classes along with the concrete classes completely independently so like I've showed previously when you want to have your first abstract class defined certain roles and then create a concrete class that adds additional roles for the concrete televisions and then you're going to have another abstract class it's going to have a reference to each one of these devices and it's going to define the abstract methods that will be defined in the final concrete remote controls that you create so hopefully that made sense if it doesn't I'm going to jump into the code and explain it in a completely different way so on with the code alright so the first bit of code we're going to create here is entertainment device and this is going to be the abstract class is going to represent every single device that we're going to be creating abstract class entertainment device and sometimes this is created as an interface but I never thought it really made that much sense that way so this is the way that I normally implement these things so I'm going to say device state and all the code is available underneath of the video and a link so if you want to check that out and we're going to define some different fields inside of this that every single television as well as remote that comes thereafter is going to have and mainly device state is going to either be the current channel that the device is on or the current chapter if we're using a DVD and then max setting is going to be the map some of channel number possible or the maximum chapter and then volume settings just going to represent volume setting so all right so now that we got that all set up we're going to define those methods those abstract methods that are going to need to be defined so we're going to say well what is going to happen when button five is pressed in the concrete class that follows it didn't have to implement that and what happens whenever button six is going to be pressed then on top of that just to show that everything is going to be working we're going to come in here and create device feedback and all this is ultimately going to do is spit out information in regards to the current channel that we're going to be on and we're just going to come in here and go device state if it's greater than max setting or device state less than zero and that remember the device state represents either the current channel or the current chapter we're going to set the device State or the channel or chapter back to zero so that's what we're doing with that guy and then we're going to print out a message and we're just going to say on device state and that's going to shoot out either the channel or the chapter and then remember we said that every single television is going to have a volume so we're going to go in here and go public void button seven pressed and that is going to represent the volume being increased so we're going to go volume level plus plus and jump up here and change this to level because I like that and it better improvin this a little bit and then we're gonna come down here and type in volume at and volume level and there you go so there we are we're setting just those buttons because everything that comes afterwards is going to have a capability of raising or lowering the volume and then button eight is going to decrease the volume and we're just going to leave that exactly that way so that is our abstract class that we're creating called entertainment device which every single device will be based off of so now let's go into TV device and let's actually create one of these guys going to copy this and jump into TV device alright so now we're in this and this is called the concrete implementer while entertainment device is represented just as the implementer and we're going to go that's just jargon by the way public TV device extends entertainment device then we're going to come up here to TV device and we're going to say okay we want to implement on implemented methods I'll click on that there we are and now we got all those in there but then on top of that we're going to create a constructor for this public TV device int new device state and ant new max setting so that all the different devices are going to have different device States or maximum settings whether it's channel or chapter if it's a DVD and then we're going to go to a device state which is represented back in the superclass device stay like that and then max setting is going to be equal to a new max setting and there we are so we have that all defined so then we just have to come in here and talk about how TV is going to be different from the DVD so I'll print line channel down so that's perfect copy that and whenever button six is going to be pressed we're going to say channel up and then I'm going to go device state - - and device state plus plus and I'm not going to worry about the DVD part that is going to extend entertainment device if you want to see what that code looks like you can look underneath the video but amazingly going to focus on the different swings between the TVs that are going to be created and then the remote controls that are going to follow thereafter so that we need to come in here then and create our abstract remote and I'm going to call that remote button dot Java so let's click on that and this is called the abstraction layer and it's going to represent numerous different ways we can use this remote to work with all the different types of devices so I guess your homework would be to go in there and take a look at how the DVD would be represented here so another abstract class then we're going to need a reference to the generic device using composition and I'm going to label this as private entertainment you guys I'm just going to call it the device like that then I'm going to create a constructor remote button and it's going to be passed one of the entertainment devices I'm going to call it new device then just go the AA device that's inside of this guy is going to be equal to the new device that's passed over to it got all so then we're going to come in here and reference all of the different methods that were defined for each of the devices so if you go back into TV device you see here button five pressed as well as button six was pressed so let's jump back over into remote button and let's define those guys so we're just going to go public void button five pressed and we go the advice and we're going to just call button five pressed inside of it so that's how we're bridging that and then we're also going to reference button six being pressed right like that and then if we want to also implement device feedback which is going to either spit out the chapter number or the channel number that is currently set for the device we're just going to go device feedback like that and then just go the device and call device feedback and then what we need to do because this is an abstract class is to find all of the things that those remotes that are going to implement this abstract class are going to need to use or extend anyway so we're going to go public abstract void we're going to say that each one of them needs to define what happens when button nine is pressed and there you did you just did that so now we're going to go in and actually create the individual remotes so we're going to need remote button which is the generic abstract remote and then I'm first thing I'm going to do is the TV remote that whenever nine is pressed is going to mute everything so let's create this and this is called the refined abstraction again there's some jargon for you and we're going to go public class TV remote mute extends and there's that abstract class and then this guy is going to say hey you need to implement a couple methods here and there they are there's a constructor we're going to need to set and basically we don't need to do anything for that just get rid of it altogether and then we're also going to need to implement another method and that is what happens when button on is pressed and real simply we're going to come in here like that and we're going to print out a different message on the screen and it is going to be TV was muted say just different layers of functionality everything abstracted out and everything can be changed without affecting absolutely anything else and there you are now we're going to come in and implement the other version of our remote that is going to pause the screen whenever the button 9 is pressed and we're going to call that guy TV remote pause job paste that inner remote pause extends remote button and everything's going to be the same there then this is going to be pause again everything else can stay the same and then TV was paused okay now we go in to test the remote to see that everything is working so we're in test the remote and I'm going to go remote button again I'm going to be referring with an abstract class here I'm gonna call this saw TV and then go new TV remote mute and right here I'm going to go to new TV device see this allows me to use multiple different types of devices not even just the TV devices if you wanted to implement a DVD or whatever but it's tons of functionality then we're going to create another one I'm going to call this the TV 2 and then just call this pause like that and it's also going to create another television and just so you can see what it would be like if you would want to create a DVD it's going to operate in much the same way so I'm going to go remote button again except this would be called DVD and this is then I'd have to create a different remote DVD remote like that instead of creating remote pause and so forth and then I'd have to go in here and go DVD device and then let's say our DVD that we create has 14 chapters anyway the codes underneath the video if you want to play around with that and you should if you want to completely understand the way this guy works okay so now let's go in here and go system out and check this guy out so first thing we're going to do is test TV with mute that's the first test we're going to do then we're gonna go the TV and we're going to say button just see right here 5 is pressed just click on that what's going to happen when button 5 is pressed and you can see right here button 5 is going to make the channel go downwards and button 6 is going to make it go up and just so if you don't remember the remote that is going to change 9 into mute is right there and then this one right here is going to pause live television as if it's in a DVR okay hopefully that makes sense if you have any questions leave them down below okay so this is the TV that has mute and we could also go the TV and then go button 6 pressed and there's that then we can go the TV button now this is the one that's going to change but 9 has been pressed and there you go now we're going to be able to look at the difference so let's go and copy this guy and this time we're going to perform a check for the test TV with pause and how it's going to differ from the TV that has the mute so we're going to go the on TV 2 and then we're going to go button 5 pressed there's button 5 and then if we would just want to do something a little bit different TV 2 and let's say we want to do a button 6 pressed which is going to make the channel go up and let's have the channel go up multiple different times ok so there's like four times and then to also provide or cover what happens when button 9 is pressed on this there's that and then on top of that if we want to get feedback on exactly what channel we are currently on device feedback course is the only option there I'll save so let's execute it see what happens in there you can see here's the TV remote that is specific to the device that has the capability to mute everything and here you can see channel down channel up and then whenever 9's press TV was muted you can see likewise TV was paused whenever button on was pressed here and you can also see that we made the channel go up multiple different times and here is our feedback that we are on channel 4 so that is the bridge design pattern like I said I go into much more detail in the code that's on the website if you want to look at it further and of course if you have any questions or comments leave them down below the last till next time
Info
Channel: Derek Banas
Views: 168,685
Rating: 4.7220545 out of 5
Keywords: Bridge Design Pattern, Bridge Design Pattern Tutorial, Bridge Pattern, Design Pattern Tutorial, Design Patterns Tutorial, Design Patterns
Id: 9jIgSsIfh_8
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Mon Oct 01 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.