State Design Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well loner net and welcome to part 20 of my design patterns video tutorial today we're going to talk about the state design pattern so what is the state design pattern it allows objects to alter its behavior when its internal state changes and in doing so the object will appear to change its class just like in previous design pattern tutorials this one's all about composition and you're going to have three main parts and this is going to be probably the most confusing slide you see in this whole entire presentation and of course tons of code is going to follow this first you're going to have what is called the context or the account what it's going to do is maintain an instance of a concrete state subclass that is going to define the current state and if that makes their sense - it will when I start showing you pictures here in a second then you're going to have the state which defines an interface for encapsulating the behavior associated with a particular state of the context and then you have the concrete state each subclass is going to implement a behavior associated with a state of the context so let's look into some pictures and make sense of this all right in this example I'm going to talk about an ATM machine now what you need to think about is what are all the states or possible situations an ATM is going to find itself in now I really dumb this down to make this easy to understand but basically an ATM is going to have roughly four states it either has a card inserted or it doesn't have a card inserted it has the correct pin entered and it either has cash or it doesn't have cash in this situation we're able to combine everything down to the point where it has no cash now whenever you're trying to implement this state design pattern you want to try to get as few states as possible otherwise things can get quite confusing then what you want to do is think about all the different ways the user can use the ATM they can insert a card they can eject a card they can insert a pin and they can request cash and that is going to be the basis of how we're going to implement this state design pattern and here you see the actual implementation here is going to be the interface this over here by the way is the context which we talked about previously if you want to rewind and look at that again but this is the interface that everything is going to be based off of and what did I do here I just listed all the methods that a user is going to be able to do in regards to using a ATM machine then from that I'm going to take all the possible states and throw all those methods inside of these states so these three states down here are also going to have these methods inside of it and then based off of which state or class is going to be defined insert card is going to do different things based off of what it is then over here what you're going to do is you're going to store the current state in ATM state and then you're going to define all these different classes right here and then you're just going to simply based off the state of the ATM machine change this value up here also we're going to monitor inside of a field whether there is still cash inside of machine and also we're going to monitor using a boolean whether the correct pin number was entered then on top of that what we're going to do is allow the user to change the state based off of things that go on over inside of here and that's what's going on with set ATM state and then these guys insert card eject card insert pin and request cash what they were going to do is whenever they're called simply send the user to the specific method that is specific to this state that is set here now hopefully you're getting about 30 percent there and understanding the state design pattern so now we're going to get into the code and explain it step by step okay so we're into the code area and the first thing we're going to do here is define our interface which is going to be called ATM State Java and all of the code is underneath the video and you should probably get it because then you will totally understand this stuff so like I said before what are we going to do here well we're going to define all of the ways that the user is going to be able to work with our ATM machine so they're going to be able to insert a card they're going to be able to eject a card they're also going to be able to insert a pin and then we're just going to go in here and go int n entered and then they're also going to be able to request cash and this is also just to be simple here I'm just going to have this be an int because I've never seen an ATM shoot out pennies and there you go there is your interface that's all you're going to have to do with this guy now what I'm going to do is go into ATM machine Java and start playing around with the context or the account that we refer to right here see context account that's going to be our actual ATM machine so back to the code alright now inside of ATM machine we're going to define all our different states and I haven't defined these yet so error message is going to pop up all over the place so we're just going to go coffee that and there's basically going to be four states so it's going to have halves card and then there's going to be no card and then we're going to have as correct in and then we're going to have ATM out of money so those are going to be our different guys we're going to work with and then this the actual ATM state that's going to be stored inside of here is going to change its state value based off of conditions then we're going to go into cash in machine and just again just keep the simple I'm going to set this to 2,000 and then we're also going to have a boolean inside of here I sense a correct pin entered and I'm going to start off with false because you know there's no pin entered whenever they first start and then we're gonna go public ATM machine create a constructor for this guy and then we're going to have all of these guys initialize themselves so paste that there and then let's just get rid of all that okay so now we just have to initialize them is equal to new as card and that's going to be past this which is a reference to itself da-da-da-da-da-da and this is going to be no card and then we're going to define these in a second here and this is going to be as pin and then this last one is going to be no cash and then we're going to also come in here and go ATM state and give ATM state an initial state of no card stick with me here if you're not quite getting it it'll catch on it's all composition just like before and then we're going to come in here and go if cash in machine is less than 0 ATM state is going to be equal to ATM Oh money and then we need to set all of our different states so we're going to also allow them to change the ATM states meaning the other states whenever certain actions occur like if we run out of money we're going to change our state to no cash so new ATM state and that's going to be passed over new ATM state and that'll be set and then we're going to go public void set cash in machine we're going to have to allow them to set the value for cash in machine so that we also know if we're running out of money and that's just gonna be cash in machine is equal to new cash in machines the only thing confusing about this is the number of different classes you need just like most design patterns if they hang you up you know the reason why is you're using more classes than you're used to and that's just something you have to work with and get over because anyone who's new to writing more advanced code is going to have a little bit of a problem struggling with writing so many classes but ultimately it'll make your code better and it will make everything much more understandable because look how simple this is and then what we're going to do is we're just going to pass it to the right state see ATM state is going to be changing and the way that ATM state is going to be changing is with this guy right here so all this guy is going to do is take whatever you send to it and pass it along to the new state that is being defined by the ATM and that's you know either I have card I don't have a card I have a pin or I have no cash okay so don't let that confuse you then we're going to go eject card and then what's this going to do it's just going to pass it to the right place like that and we're going to be going to be doing the same thing over and over again here for all the possible ways that the user can interact with this so we're going to go request cash and then this is going to be in cash to withdraw and then what's going to go on basically you're going to pass that to the method that's defined in the current state that it's defined and then the final thing is insert pin and then this is just going to be an integer and it's pin entered and these are all the things that are defined in ATM state say right there so that's all that's going on with that I just need to define them right and then of course pass this along to the right method inside of that class that we're going to define here in a second and then we're also going to have to provide a way for them to get the current state so we're going to say get yes card state meaning do they have a card or not returned as card so that's going to allow them to change States as well get this is going to be no card state and then this is going to be changed to no card get as pin and this is going to return as correct in then the last one is going to be get no cash state which is going to return a reference to the object that we create ATM out of money and all of these guys just come from here so those are just going to return those state objects that you define there that's all these four things do right there so get rid of all that white space and we are done with the context and of course get rid of that and get rid of that there we are I'll save and everything is looking dandy except for the fact that we need to create all of these guys has card no card has pin and no cash so let's go into has card dot Java and there we are and you can see here implements ATM state and a foot put the mouse over this it's going to say Adam unimplemented methods and then I just need to come in here and think about logically what I need to do in all these different situations now one thing I'm going to have to do is I'm going to need to store the context which is the actual object that I'm going to be working with here and also I'm going to have to come in here and go public as card which is going to be when they pass over the ATM object that we are required to use so that we're using the same ATM object and not you know messing that up so we're going to pass a reference to our ATM machine over to this we're going to take that whoops we're going to take this guy and assign it to our temporary holding cell make some changes to it okay so if you have a card and then they try to insert another card what are you going to say this is all logic from now on this is piece of cake no problem totally understandable you're gonna have to say you can't enter more than one card say that's all you're done and that's it that's all you need to do now we don't need to change state because well it has a card they tried to enter a new card that the state didn't change you just gave them a warning sign okay so what are we going to do if they try to eject the card in that situation we're gonna print out a different message let's just say card ejected there we are and then what are we going to do well now we don't have a card so we're going to need to change the state for our ATM machine so we're going to go ATM machine which comes from this guy right here and then we're gonna have to go set ATM state we need to change the state for our ATM machine because it just changed and to set it we're gonna go ATM machine when you say get no card state pass that back boom boo-boom and now the state has changed and set ATM state is found we're in ATM machine Java and if you scroll down see set ATM state and how is the state being passed right down here with these guys say these get set it out of that alright so let's get back in here and continue fixing this off so that's all we're doing is dynamically changing the class based off of different actions that are being performed so then they come in and they say okay well I want to insert a pin well they inserted cards so that sort of makes sense so now we're going to go pin entered and we're going to check is it equal to one two three four now I've really again we're not checking anything here rule sophisticated okay so let's just say yeah they entered one two three four and that is the correct one now you could say something like if you're correct in and then what are you going to need to do well you're going to have to change ATM machine you're going to have to say hey the ATM machine correct pin entered this guy right here well it's false so they entered the correct pin so now what are we going to have to do well we're going to have to come in and change that value so ATM machine correct pin entered and we're going to set it to true all done and then of course da alright so we got that set and then what do we need to do well we need to change the state for this and that state is going to be gay get as pin done you just did all that right away and then we're going to come in and go else they entered the wrong pin well we're gonna have to punish them for that and we're going to come in here to this copy shoot that out there and we could say something like wrong pin and we're gonna make sure this is set to false I don't know if that needs to be done that way or not I cranked this code out pretty quickly and then I'm just gonna come up here doink and I'm gonna say something like car dejected let's just copy what that says up there and then we're going to need to change the state because remember we're in halves card well if the cards ejected well what does that mean well that means we're no longer a has card state because we just injected a card and make that go through we're gonna go no get no card state change that to that that's good and then down here for request cache if they request cash what are we gonna do that's just giving them an error message and say there enter in first okay there are hash cards done so we're gonna save that and we're gonna move on to no card guess what we're pretty much going to do the same thing so go to no card add-on implemented methods click on that that opens up and now you got to define all the different things you want to do here differently so let's just come in here doctored I'm going to do this copy that jump back over in the new car java paste that in there because I need a copy or a reference to the ATM machine and this is just going to be changed to no card so that's all fine and this is going to be perfectly fine as well now if there is no card and they enter a card what are we gonna do we're gonna have to print a message out to them so on that message is gonna be please enter a pen and then what are we going to do we're gonna change our card state to yes card state because we got card because they just inserted a card so you know that's great and then eject card well what are we gonna do here well we're in no card so that doesn't work so we're gonna print out a message enter a card first can't eject a card if they don't give us one come down here and just to make this simple I'm going to do the same thing and record first because you can't insert a pin until you enter your card and then request cash is also going to be exactly the same thing so just coffee Don come and arm tada boom you're done so there you are no cards done if I'll save it so what happens whenever they have a pen well that's where has pinned our java comes in when jump inside of here again come in here add on implemented methods one Don with that as pin that just simply means that they entered a pin and it worked out gonna do this and then we're going to change this to has pin like that leave everything else the same and then down here insert card well that's going to be already entered a card so I'm actually going to copy this from that jump back over into has pin they already entered a card and they already entered a pen well guess what we don't want another card set shoot that in there eject card is gonna do the same exact thing as has card did over here so I'm going to jump over here copy that and we're gonna give them the card that they just entered even though they also entered the right pin this is all just pure logic insert pan well they already entered the tricked pin so let's just get rid of that and make this simple already entered pin and then down here request cache well they entered the right pin so they should be able to come in here and request cache so we're just got to make sure they have enough cash so we're going to say if cash to with draw is greater than which is what they passed over to us and then we're going to go ATM machine and we're going to check cash in machine to make sure there's enough cash in our machine before we go and start trying to give them cash it doesn't exist and then we're going to come on here give them a message like this they tried to ask for more cash than what we actually have so we're gonna come in here copy that and I'm gonna say something like don't have that cash there you are and then I'm gonna eject the card and shut set their status back to no card else I'm gonna give them a little message that tells them how much cash was ejected so let's just copy this paste that in there and then I'm going to go cash to withdraw + and say something like is provided by the machine and then I'm going to go into ATM machine and set cash in machine I'm just accessing that field back there I'm going to go ATM machine dot cash in machine - cash - withdraw so I'm going to make sure that that is set up properly and there we are and then we're gonna go your card is ejected and then set the status back to get no card so pretty simple there and then maybe as one additional little change we could throw something in here like if ATM machine cash in machine is less than or equal to zero then we're going to also come in here and change the state for our ATM machine and that state is going to be set to get no cash state see the ATM machine is officially out of cash and I think that's it yes it is ok so file save that and then we just need to go into the last thing which is going to be no cash dot Java this is going to be really easy let's copy this guy right here again we're gonna do pretty much exactly the same thing paste that there we got a copy of our ATM machine gonna change this to new cash that is all gonna stay the same and then no cash is gonna say hey you need to implement some methods well okay here we are just implemented them and then this is gonna be really simple because I'm basically going to be giving it the same message okay so if they insert a card and we don't have any money in our ATM machine what are we gonna say we don't have money okay so we're just gonna print that out that might cause a run on the bank but either way that's what we're gonna to eject the card well we're just in say we don't have money you didn't enter a card so you know both ways we're not gonna be able to do anything for you insert pen we don't have money and keep it extremely simple across cash guess what we don't have any money we're going to of course keep everything set to no cash because that's very important we have no reason to change that so let's test it out see if we did it right going to go into test ATM machine here I already did the code for this basically what we need to do is just create ourselves an ATM machine we need to insert a card eject a card insert a card enter a pin request cash insert card enter a pin and let's just see what happens so it's executed and you can say please enter a pin say insert card that's what it shoots out card ejected please enter a pin whenever they insert the card again this time they entered a pen say correct pin right there they requested two thousand dollars that's the maximum amount we have two thousand provided by the machine automatically ejects the card then you insert the card and it says we don't have money and then it comes down here and you enter our pin and it says we don't have money so there it is it's a state design pattern anything didn't make sense leave a question or comment below of course requests are always welcome of the last till next time
Info
Channel: Derek Banas
Views: 201,114
Rating: 4.8311925 out of 5
Keywords: State Design Pattern, State Design Pattern Tutorial, Design Pattern Tutorial, Design Patterns Tutorial, Design Pattern
Id: MGEx35FjBuo
Channel Id: undefined
Length: 20min 51sec (1251 seconds)
Published: Tue Oct 16 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.