Command Design Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 12 of my design patterns video tutorial today we're going to talk about the command design pattern now I'm going to show you this in numerous different ways I'm going to explain it as a simple definition I'm going to explain it as a walk through just using words then I'm going to show you a UML diagram then on top of that I'm going to show you all of the code and then on top of that all the code is available underneath the video so if you go through all five or six of those steps I guarantee you will understand the command design pattern so let's get into it so what is the command design pattern well the basic definition is the command design pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time this information includes the method name the object that owns the method and the values for the method parameters now if that didn't make any sense let's move on and explain a completely different way basically what it allows you to do is store lists of codes that are executed at a later time or many times and on top of that normally with the command design pattern there's a capability for you to undo commands basically what happens is the client or the application part of your program says I want a specific command or a list of code to be executed whenever a method called execute is called on one of these encapsulated hidden objects and then an object called the invoker whenever it is invoked to perform a command to set it in motion transfers this command to another object that is called the receiver and the receiver has the actual code that you want to execute and whenever it gets the command from the invoker to execute it it executes it so basically if we have this object down here called turn TV on this is actually a command this command is defined then device button which is another class or object that's going to be created whenever it is called it sends turn TV on of course there's going to be interfaces between all this we can use polymorphism so whenever turn TV on is called that method then will be executed by the receiver or television another object and here is turn TV on so there's another explanation so let's look at it from another way okay here is the client again and it's going to define a device which is going to be an object that's going to contain a whole bunch of different methods that can be run for that very specific device object it's going to define a command that they want to be issued this is again the application part of the client and the command object has one method inside of it called execute you can see everything over here so let's bring in the invoker to this and then explain everything that goes on okay here is the invoker which is just going to be an object that is going to have a method inside of it in this situation it's going to be called press now when press is called it's going to call execute on the command object here is execute as I am refering to it of course it's going to refer to it through an interface for polymorphism but either way so press is called on this object called the Vice button and it calls execute on turn TV on then what happens is execute tells the television object that is right here to execute the on method which is up here in this television object in essence that is the command design pattern device button is called specifically press a method inside of it or at least that's what I'm going to use then execute is called on a nother object called the command object execute is over here whenever execute is called all it does is it says hey my job as an object is to turn televisions on so what I'm going to do is I'm going to tell the television object to execute the method on that is inside of it so that's sort of an explanation in that regards so what are the benefits of the command design pattern well it allows you to set aside a list of commands for later use like I've mentioned before on top of that a class is a great place to store procedures that you want to be able to execute multiple times you're going to be able to store multiple commands in a class to use over and over and on top of that like I said before most of the time you're going to implement undo procedures so that you can undo previous commands the only negative with the commands on pattern is that you need to create many small classes to store lists of these commands so that's a basic rundown so let's just jump right into the code alright so the first thing that I'm going to need to do here is to create an interface that is going to be used to create all of my receivers that sounds really complicated but it's really not so I'm going to create an interface is going to be called electronic device and basically what I'm going to do is implement a remote control sort of system here so real simple we're just going to go public interface electronic device okay so there's going to be our interface that we're going to build all our receivers from and then I need to define all of the different methods that each one of these devices is going to have so on its going to be able to turn itself on it's going to be able to turn itself off it's going to have the capability to turn the volume up and it's also going to have a capability to turn the volume down and there you are and of course make sure you spell great so there's our interface that we're going to use to make all of our electronic devices or receivers and so let's just jump right into it and let's make a receiver called television Java so here's television dot Java we're just going to go public class television and I know I said all the time but really at least for myself I wasn't able to completely understand this pattern until I saw it in print so of course I provide you that option underneath this video ok so just going to implement that electronic device and then we're going to come in here and Eclipse is going to allow us to add unimplemented methods so I don't have to type all that stuff out then inside of here I'm going to create myself a private method and it's just going to contain a value for volume and it's going to start out at 0 and then I'm just going to keep this really simple I'm going to say whenever these are called of course you could have many different things occur here but I'm just going to say TV is on just to keep it really nice and simple and then in this situation TV is off TV volume is at and then we'll just throw volume inside of there which is that private variable that we have and TV volume is that and throw that inside there as well and then all we're going to want to do here is just go volume plus plus to increment that and then volume negative negative there you are you just created yourself a receiver called television that's going to be able to implement these very specific commands basically all we're doing here is we're making commands very unspecific and eventually they will lead here to print and execute the actual real commands themselves that's all you're going to have to do with that so now let's go and our command interface that every single command is going to have to implement and it's going to be simple as well we're just going to go public interface command and this is going to be extremely simple we're just going to say public void execute everybody that wants to use this command interface must have an execute option that's pretty simple and guess what you're done so now let's start creating some very specific commands we're going to go into turn TV on Java right there and we're going to go public class turn TV on implements commands of course because that's what we just created and then have it come in here and put in my execute method so that everything is nice there and then inside of this this guy is going to need to know okay well what exact role electronic device do you want me to be working with so I'm going to store that inside of here and then turn TV on going to create a constructor here and it's just going to be passed an electronic device to perform this command on except I'm going to change this to new device and then just go the device is equal to new device which is going to be passed to it so pretty simple then we come down to the execute part of this guy and all it's going to do is say okay that device that you sent me since I am known as the turn TV on command what I want to do here is call the on method for said device boom you're all done so all we need to do you just implemented your first command object inside here and guess what turn TV off is going to operate in much the same way so where is turn TV off there it is turn TV off dub Java click on that paste inside of their turn urban TV off is going to be the name for that and then we're just going to come through here and make a couple little changes but pretty simple stuff just about everything is already done there and then down here since it's the turn TV off we're going to call the off method on the device in case you forgot let's just go over to television Java say all it's doing is coming here and calling these very specific methods for these very specific objects so no big deal so let's get back to turn TV off to a job look at it again and that's it you're done we just did that as you can see remember I said one of the negatives is it has a whole bunch of little classes now that can also be a positive so let's file save that and let's go over to turn TV up dot Java paste that in there turn TV up gonna implement command just like before change that to up everything else is the same and then down here we're just going to go the device and we want to call the method vol up there you are just did another one and if you want to you can go and also create turning TV down but in this situation I'm not going to do that I'm going to show you in a minute how to use an undo command to do exactly that so what does that leave us with that leaves us with the invoker and it is going to have a method it's first off it's name is going to be device button because we're using sort of like a remote sort of thing here so we're going to come inside of this and it's going to have a method called press that when executed is going to cause the execute method to be called and then of course the execute method for the command interface then calls the method assigned in the class that implements the command interface okay so just go public class device button there that is and then it's going to be passed command of course in a generic sort of format and we're going to go public device button command new command is going to be passed over to it and I'm just going to go to the command just like I always do is equal to new command then we're going to create public press that I've been talking about so much very very complicated method what's it going to do it's going to say the command I want you to run execute just like I said before and then it's going to worry about command and all of the different objects so they're implementing the command interface or then going to just execute that execute command again let's click on command what to do forces all commands to have an execute method here's turn TV on there it does whenever execute is going to be called on this method it instead calls another method for our device and whereas our device wellhere's television Java there it is you just have to wrap your head around it just look at it from a couple different directions and you'll get it let's get back in a device button and yes indeed it's done as well so then what do we need to do well we're going to have to create something else called TV remote dot Java and in essence what TV remote dot job is going to do is it's going to return the type of device that we're going to use now I chose to keep this fairly simple but of course you could have it work with multiple different devices and I'm going to show you another way to work with a completely different device other than television here in a second basically what I'm going to do here is just go public static electronic device get device okay so it's going to have this static method inside of it and this situation it's just going to return a new television which is just going to say hey we're just going to be using a television object here as our receiver to handle everything so that's pretty simple so now we're going to go into play with remote job and this is the actual application and it's going to actually create all this stuff so I need a couple different things here I'm going to go java.util.arrays and then I'm also going to use a basic list so just chop off the array part and there you go now you have all the libraries that we're going to use here I'm going to go public class play with remote and then what are we going to do is since it's our application we're going to have public static void main orgs then we want to get the electronic device that we want to use and we're just going to go new device is equal to TV remote dot get device and what is that going to do for me let's just take a look here's TV remote it's going to return a television so it's going to say hey you want a device well you're going to work with television now of course we could figure out different ways to come in here and use all the other different devices and in fact I'm going to show you another different way to do that and then what are we going to do we're going to go turn TV on and this is going to be called the on command is equal to new turn TV on here we are defining a command that we want this device to be performing here we're going to send the device over and have it execute and like I said before turn TV on just going to contain a command to turn on the TV when execute is called on this command object and then of course it's going to execute the method on inside of the television device just let the information wash over you'll get it ok so then we're going to go device button this is going to be the receiver and it is going to perform certain actions whenever it is called so we're going to go device button and we are going to send it the on command now of course it just sees it as a regular command but the receiver later on will figure it all out make everything work beautifully okay so we everything's set up we have our button which is going to set everything into motion we have our electronics device it's going to contain all the methods we want executed and then we have our very specific command so whenever the button is going to be pressed on pressed there we are just press the button on a remote control then what's going to do is it's going to send a generic command over that's going to be on command that isn't really going to be generic because it's going to be called turn TV on which isn't really going to be generic because it is of type turn TV on which of course you know does one thing two devices it calls the on method on them so let's file save it and see what happens execute and you could say TV is on that's all it took sounds more complicated than it is so how complicated is it to come in here and now turn our television off well basically we're just gonna copy this and change a couple different things here I'm just going to divide this up a little bit so it's easier to tell what's going on I'm going to paste that inside of there now if we want to perform a different action what are we going to need to do we're going to have to change the action that's performed so now we're going to say turn TV off and I'm going to call this guy the off command and then of course we're going to need to change this to off as well however we're still going to pass the TV object so that doesn't need to change we're going to lock this off of here because we've already defined it and here we're going to pass the off command which we just created and here on press doesn't care what type of command you're sending it's going to work either way so let's execute that and you can see I turn the TV on and then I turn the television off so let's see what happens whenever we come in here and start playing around with the volume and even better can we call press multiple times without things flipping out yes we can now if we want to make the volume go up we're going to go turn on TV up just change our type on our command and then in here I'm going to go a volume up command turn TV up we're still going to use a generic device don't need to worry about that on press too course is going to change here and then we're just going to send the right command to it like that and then we're going to not only press the button once why don't we press it a couple times and execute and there you go the volume has been increased see one two three it started out at zero so pretty cool really implementing a remote control here so now let's throw another device into the mix just to show you how simple that is to work with we don't have to touch electronic device in any way as long as we don't need to have any other methods implemented let's say we want to have radio dot Java created how hard is that well we're gonna go public class radio implements electronic device and just to save ourselves some time I'm gonna jump inside of here I'm gonna say okay I need all this stuff copy jump back into radio paste there you go because the radio is also going to have a volume so now all we need to do is change this from TV to radio that's all we're going to need to do of course you could do a lot more but that works and then we're also going to create the capability to turn off all the televisions and all the radios all at one time and to do that we're going to have a class called turn it all off Java and to get that to work we're going to go import java.util and we're going to use a list to cut everything off public class turn it all off so this is like a master off switch to shut everything off at once and this is going to be implement the command again and what is it going to force me to do pretty simple it just wants me to implement execute because everything that implements command must do that so what am I going to do here since I want to be able to turn all of them off I'm going to need all the electronic devices that are currently on or set up to work with our little systems sent to me as a list so I'm gonna refer those as the devices just like that then I'm going to create a constructor public turn it all off list and what's it going to be sent electronic device so paste that and so out of there and here I'm going to say new devices like that and then to transfer all of these devices that I now want to control one fell swoop I'm going to take the new devices list that's going to be sent to me and I'm going to save it to my own personalized list all right now we get to execute and what we're going to do here with execute is we're going to shut all of them off using an enhanced for-loop so they are all electronic devices so that's cool and we're going to say device individually and then the the device is of course is our list and then to shut every one of these devices off we just need to go device dot off there you go it's going to flip through every single device in the list and shot every single one of them off that's pretty cool so let's jump back over into play with remote dot java and let's add those capabilities which is going to be pretty simple we're just going to come in here make another dividing line inside of there so it can have a little bit more room alright so we need to create ourselves a whole bunch of different devices we want to work with so I'm just going to go and create a new television and I'm just going to call it t V is equal to television so fun tutorial to do and then I'm going to do radio the radio is equal to new and it is a radio object then I need to create myself a list and it's going to be electronic device all devices is going to be loaded into it so I need to go new array list electronic device like them okay so get that all set up now I just need to add my devices to all devices list and the TV all devices add the radio got that set up and now what I need to do is just send the electronic devices to turn it all off where a call to run execute on this function will call off for each device in my list also pretty simple so turn it all off turn off devices is equal to new I don't feel like turn it all off again so there it is and I just need to tell it to shut off every single device in my list and it will and then I need to go device button turn them off is equal to new and then this is going to be device button so it's like I'm putting a new button on my remote control dynamically and then going to send over turn off devices to it so that it knows what devices to shut off and now that I created my new button I just need to call press on it to press that button and all the devices will be shut off let's execute it what should happen there it is so a turn the TV on turn it off turn the volume up to 3 turn TV off and radio off all in one fell swoop so now let's implement our undo command I'm going to do this to a certain extent I'm going to leave part of it for you to do as homework but either way it is common to be able to undo commands using a command pattern it's not required but it's just something that is very common that you will say and to do so the device button will have a method called undo which will perform the opposite action or whatever the normal command performs and implement on do you're just going to need to add it to every single class in which you have the execute method so what are we going to need to do here well we're going to have to go into command Java which we created already and we just need to implement the capability to undo commands so I'm just going to change execute in a situation on do file save and there you go now you can undo commands now you're just going to have to go through all of your individual commands and also implement on do with those commands so turn it off now all of a sudden is going to say hey guess what you need to do undo now so real simple I'm just going to come up here and what is the opposite of turning advice on well of course it's turning device off and look at that you just did it you just implement it undo in what 3 or 4 keystrokes that's pretty cool I'm actually just going to copy this guy right here and then I'm just going to go into all the other different things here so turn TV up let's go into there and let's bounce down here paste that inside of there and what is the opposite of turn TV up well turn our volume down volume down and there your honor just implemented that not too hard and then we also need to go into device button dot Java because we're going to add ourselves a new type of button here and it's going to be a simple button we're just going to copy press and instead we're gonna say want to give them the capability to press undo so there we are and then the command here it's going to be changed to undo which we already created in our command interface it's also pretty easy I could then go in to turn it all off turn it all off there it is turn it all off the java bounce inside of there and pretty much do exactly the same thing that executes does except we're going to do it using undo instead of execute so tab and undo and instead of off we're going to switch off to on see just does the opposite what it did before and now we can jump back in to play with remote java right here and let's start playing with the capability to undo stuff so instead we're gonna go turn them off press undo like that I'll save and now it's going to undo the last command which was to turn everything off and then stud turn everything on so that is a whole bunch of different ways to look at the command pattern like you said before I'm going to give you homework this time let's say we want to do undo on every single command there is out there I'm going to give you a hint on how to do that look into linked lists and saving all your different commands in linked lists and then using add first to add your commands as they execute to the beginning of the linked list and then what you'll be able to do is go through the linked list and just do an undo command on every single command that was done last through first rather than the other way around either way leave questions or comments below again remember the codes underneath the video if you're unclear on anything otherwise till next time
Info
Channel: Derek Banas
Views: 244,063
Rating: 4.8793707 out of 5
Keywords: Command Design Pattern, Command Pattern, Design Pattern, Design Patterns, Design Pattern Tutorial, Design Patterns Tutorial
Id: 7Pj5kAhVBlg
Channel Id: undefined
Length: 23min 40sec (1420 seconds)
Published: Mon Sep 24 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.