02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is

State

An application doesn't need to always be aware of it's state. Awareness tends to add more complexity into an application (such as multiple conditions). By extracting the different states into separate implementations all that is left for the application to do is update it's state to change it's functionality. A great example is a GPS and how it changes between day navigation and night navigation (by car, foot or bike).

Video Transcript

00:01Let's get started with our last design pattern.
00:05And this last design pattern
00:07is gonna be the state design pattern.
00:10The state design pattern, the idea behind it
00:13is that the different states of an application
00:17demand different reactions.
00:19That we know.
00:20Those different reactions in turn
00:22start turning your application
00:24into a really complicated creature
00:26with a lot of conditions.
00:27If you have daytime conditions, for example,
00:29in the GPS application, and nighttime conditions,
00:33and then there's bicycle conditions, and car conditions,
00:36and walking conditions, and rainy day conditions,
00:39and so on and so forth.
00:42Instead of having all these different conditions
00:44deeply integrated into the state of the application,
00:48a smarter way would be, first of all,
00:51again, the more aware the application is
00:53of all these different conditions,
00:55the more conditions there are in the applications,
00:57the more complex it gets.
00:59So our concept is to extract the states.
01:02That extraction enables us to reduce
01:04the complexity of our application
01:06because our application doesn't need
01:08to be aware of these different states.
01:10It just has to switch states.
01:11Now, with that said, in this sample
01:13that we're gonna show you,
01:14it's probably not gonna be the perfect sample
01:17for a really complicated scenario.
01:19That's something you're gonna have to confront
01:20on your own in a real world application.
01:23But you're gonna know and exactly see
01:25how to work with the states in this lecture.
01:28So let's jump right into it, and let's get
01:31to exploring this relatively long lecture
01:33that is super, super early cool.
01:38More often than not, the state design pattern
01:41is used in a scenario where you don't want
01:44your object to be aware of the state,
01:46or it doesn't need to be aware
01:48to know how to interact with it.
01:51In this lecture, we're gonna go ahead
01:52and create something that is inspired
01:54by the state design pattern,
01:56but it's probably not very practical.
01:58What we're gonna do is we're gonna create
02:00a light system that is gonna look something like this
02:03where we have a light, and then the light
02:05will change colors.
02:07So let's go ahead and start building it out.
02:10To do that, I'm gonna start off by creating
02:12a function that will store everything
02:15related to the red state.
02:16It's gonna send in the object of the actual circle
02:20that's being created into it,
02:22and that way you could manage it and manifest it.
02:26All right, so the next step is I'm gonna create
02:28some private variables here.
02:30I'm gonna have the on state, which will be red,
02:33pure, beautiful red.
02:35The off state is gonna be very red,
02:37but it's gonna be RGBA.
02:39We're gonna use the red, green, blue, alpha channels,
02:42setting it to red, blue at zero, green at zero,
02:47and 25% opacity, or alpha.
02:51The next stage is I'm gonna, oh, you know,
02:53let me store this in the meantime this way.
02:57The next thing that I'm gonna do is I'm gonna create
02:59basically the starter function,
03:00and I'm just gonna append it to our function.
03:04Start, to our object that we're sending,
03:07that we're creating that red state.
03:09So I'm gonna go ahead here and create that function,
03:12and what are we gonna do with that function?
03:14Because this object is literally one of those circles
03:17or squares or whatever they would not be,
03:20I'm gonna go ahead and directly manipulate the color value.
03:23So let's go ahead and do that.
03:25So I'm gonna go ahead here in my red state,
03:28and I'm gonna go ahead to my object
03:30and set the color to be the on state color.
03:35Beautiful.
03:36The next step is I wanna basically set here a timer
03:41that will then trigger the next state.
03:43Now before I even continue this,
03:45the idea here in states and the actual trigger mechanism
03:49of the next state could be done in many ways.
03:52I opted to do a chain like we did previously
03:55in this chapter.
03:56I could have used events,
03:58I could have used something in the logic,
03:59any logic that would actually trigger the next thing,
04:02because I'm mixing together right now,
04:04state and chains together,
04:06just to also practice what we've learned so far
04:09in this section.
04:10So let's go ahead.
04:11I'm gonna go ahead and basically set a timeout,
04:15and what I wanna do is,
04:16first of all, I wanna, even before we do this,
04:19okay, one second.
04:20Let's go ahead and create another private variable here.
04:23We're gonna call it next state,
04:26and I'm gonna go ahead and also create quickly
04:28another function,
04:29and I'm gonna set this with it the state.
04:32So I'm gonna call this next state,
04:34and it's gonna be a function.
04:36It's gonna accept the next state,
04:38and I'm gonna literally just go ahead there
04:40and assign into next state the value of next state.
04:45Okay, so now that I have next state,
04:47I could go back into my set timeout and continue working.
04:50In my set timeout,
04:51I'm gonna use my binder that we created
04:53in a previous section,
04:55and I'm literally gonna use that binder
04:57to define the next state to remain in its scope
05:01that it was in,
05:01and I'm gonna call the start function,
05:03literally pushing the next start of the next state,
05:07and I'm gonna have that happen after 1,000 milliseconds.
05:11Beautiful.
05:11One more thing that I wanna do
05:12is I wanna also make sure that the red color
05:14will be turned off,
05:16so I'm gonna go ahead there, set timeout as well,
05:19and I'm gonna go ahead and create here a function,
05:22and I'm gonna call the object color off to turn it off,
05:27and I'm gonna have that happen after 3,000 milliseconds.
05:31Okay, to recap.
05:32So the red state accepts the object itself.
05:35It has the on and off states.
05:37It has the next state, which we're gonna configure,
05:40and when it's called to start,
05:41it's gonna turn the light on,
05:43create a timer after one second calling to the next state,
05:46calling that start function of the next state.
05:49Last but not least, after three seconds,
05:52it's gonna go ahead and turn off its own light.
05:55So I'm gonna go ahead here
05:56and copy this red state function,
05:59or really constructor to be more accurate,
06:01and I'm gonna go ahead and name this yellow state,
06:06and let's go ahead and just update the values.
06:08So here we want it to be yellow.
06:10For here, we wanna turn this to be yellow as well
06:13in the off state, so that would turn that to yellow.
06:16For our next state, everything there is completely fine.
06:18This is completely fine.
06:19What do we wanna do in a scenario of yellow?
06:22Well, in a scenario of yellow,
06:23we just need one timeout.
06:25We don't need two.
06:26So I'm gonna go ahead here
06:27and literally just get rid of one of them
06:30and set a timeout of, let's say, two seconds.
06:33We're not, let's say,
06:34because I want it to correlate and match
06:35to the time that the red light will turn off.
06:39So that will happen
06:40that both lights will turn off at the same time.
06:42Last but not least, I'm gonna also go to that next state
06:46and trigger the start event of that one,
06:48which technically is gonna be the green state, okay?
06:52Now let's go ahead here and copy all this,
06:54and we're gonna, let me just put the semicolon
06:56where it should be,
06:57and let me just copy also the yellow state,
07:00and let's go ahead and create the logic
07:02for the green state.
07:04And again, don't worry
07:05if I'm going a little bit too quickly,
07:07because I wanna get a lot of things in.
07:09Just make sure that you go into your,
07:12make sure that you go into the source files
07:15and take a deeper look into it
07:17to understand all the different steps here
07:18that are happening.
07:20So we're gonna create now the green state.
07:22So I'm gonna set this to be on the on state as green,
07:25and on the off state,
07:26I'm just gonna set it to be green at 25% opacity.
07:32This logic stays exactly the same.
07:34This logic stays exactly the same.
07:37What else do we wanna do?
07:38Well, in this case, really what I wanna change
07:40is I wanna leave the green light on
07:42for a little bit longer than all the rest
07:44before I call the next one.
07:46Now again, this next state that we're calling
07:48has nothing to do with the state logic.
07:50This has everything to do with the chain design pattern.
07:55Most of the times you will have different states
07:57that are not actually controlling anything.
07:59They're just defining the state itself.
08:01And then something external to them
08:03will then switch states.
08:04This will happen, for example,
08:06if you have a map application,
08:08and you have a day, and you have a night,
08:10and in daytime there's certain different functionalities
08:12that happen in a certain way,
08:14and at nighttime there's a certain different setup
08:17of things that happen a different way.
08:18You don't need your application to know
08:20about the daytime or nighttime configuration.
08:23You just need to change the state
08:25and have the application continue running as it did before.
08:28In this case, we're kind of mixing
08:30between the two concepts together
08:32for something that is a little bit more complex.
08:35So okay, so let's continue.
08:36So now that we have all of our different states ready,
08:39it's time for us to start building them out.
08:41So I'm gonna go ahead and just get rid
08:43of the blue circle builder
08:45because we're only gonna be using the red one,
08:47which is actually a circle.
08:49What else are we gonna do?
08:50Let's think here.
08:52After we configure that and we only have that,
08:55then the set stage is still accurate.
08:58We don't need any click anymore,
08:59so I'm gonna go ahead here
09:00and just get rid of all the key presses
09:02and just get rid of all that.
09:05And let's start building up our application.
09:07Okay, so the first thing that we need to do
09:09is we need to get actually references
09:11to three different colored items.
09:13The first colored item is the red one.
09:15So let's go ahead and create it.
09:17We need a red item,
09:18so we're gonna go ahead and create it,
09:20and we want it to be positioned.
09:22I already configured the positions
09:24that I need it to be in, which is 400 by 250,
09:28and I want it to be the red type.
09:30Now don't get confused here.
09:31This is not necessarily red color,
09:33although it is red right now.
09:34And I'm gonna go ahead and just add it to the stage
09:38through our singleton.
09:40All right, next, let's go ahead and create the green.
09:42Now the green, again,
09:43I'm just gonna go ahead and create a new instance.
09:46It's the exact same logic as before.
09:48This logic is exactly the same thing.
09:51There's just one more thing that we wanna do
09:52even before we add it into the stage.
09:54So let's go ahead there and just put here another comma.
09:59And what I wanna do is I wanna go ahead
10:00to that green item,
10:02and I wanna go ahead and color it.
10:04To what color do I wanna color it?
10:06I wanna color it to green.
10:07So I'm gonna go ahead here,
10:08I don't want it to be fully translucent.
10:10I want it to be quite translucent.
10:12So I'm gonna take that green color
10:13that we worked on previously,
10:15and just send that translucent green
10:17to be that initialization color of our object.
10:21Next thing that I wanna do
10:22is I wanna go into our object.
10:24And by the way, I could have also done this
10:25inside of the constructor itself
10:27as the constructor is sent in,
10:29which actually sounds like a wonderful idea.
10:31I could just go ahead to that object,
10:33set that color,
10:34and then define it to the off state by default
10:37as soon as it's configured.
10:39Now, the only problem with that
10:40is that I would have to configure the red item.
10:42So I might not do that,
10:44unless maybe I wanna set here
10:46if it's in the on state or off state,
10:48but I'm not gonna play around with that.
10:49Instead of that,
10:50I'm just gonna add those extra variables down below here.
10:53All right, so we configured the green color.
10:55Let's go ahead and just copy the red again
10:58because, well, let's copy the green one
10:59because it actually has all the steps really that we need.
11:02And this time around,
11:03I'm gonna go ahead and actually create the yellow one.
11:06And let me just make sure that I don't forget
11:07to add the green one and not the red one.
11:10So let's turn this into yellow.
11:12And I'm gonna go ahead here and just copy that,
11:15update all the places where it's supposed to be yellow
11:17and update the color to be yellow as well.
11:19So that works perfectly.
11:21Again, notice that we're using red here
11:23because that's the name of the shape as we configured it.
11:25If we wanted to make it a little bit more logical for us
11:28as we're working,
11:29we could call this circle instead
11:30to make it a little bit easier.
11:31And then every time we would have to refer to it
11:34as a circle instead of as red,
11:36which could become quite confusing.
11:38All right, so we created a red item,
11:40we created a yellow item and we created a green item.
11:43All that's left for us to do really
11:45is now to create a reference to those different states.
11:48So let's go ahead and do that.
11:50I'm gonna create the red state.
11:51Let's go ahead and create a new red state.
11:54And I'm gonna send into it the red object,
11:56the red circle.
11:58Let's go ahead and create also the yellow one.
12:02And we're gonna send inside the yellow object.
12:05And last but not least, the green object.
12:07And we're gonna go ahead,
12:09sending the green object into the green state.
12:13Beautiful.
12:14So now that we have all the variables configured,
12:16all that's left for us to do is to literally go one by one
12:19and set their next state.
12:21So the next state of red is the yellow state.
12:24Perfect.
12:26Let's go ahead and set the yellow state.
12:28The yellow state, next state.
12:30What is it?
12:30It's green state.
12:31You got it.
12:33Last but not least, the green state.
12:35Next state, what is it?
12:37We're back again to red state.
12:39So there we go.
12:40Hopefully this is everything we have to do.
12:41All that's left for us to do is test it out
12:43to see if it's actually working.
12:46And we're getting warmer, but it's not right.
12:49Okay, so let's see.
12:50First of all, we're only seeing one item.
12:51We're not seeing any errors.
12:53That's telling me that we probably forgot
12:55to put them on the screen.
12:57No, we've put them on.
12:58Okay, I just didn't put their position.
13:00So let me go ahead and configure the right positions.
13:03The next item, we want to push down to 325.
13:06And the next item, we want to position at 400.
13:09Okay, let's give it another shot.
13:12And here we go.
13:13We have the three colors configured.
13:15They're all sitting in place.
13:16They're not running yet.
13:17Why aren't they running?
13:18Because we never actually started them.
13:20We never called the first start.
13:22So to do that, I'm going to go ahead right below here
13:24and I'm going to configure the first state by calling it.
13:27By calling the start command in the red.
13:30And let's go ahead and save this.
13:31And hopefully now everything should be working.
13:34We're going to start with the red state,
13:36move to the red yellow, and then it'll become green.
13:39And then right back to that initial state
13:42once that amount of time is over.
13:44So there we go.
13:45We just created another design pattern,
13:48the state design pattern integrated with the chain.
13:54In this lecture, we met the state design pattern
13:56and with it, we completed this title.
13:59I hope you had a really great time.
14:01And please write a review, share with a friend.
14:04If you have a blog, please write about us.
14:07Let everyone know about this title.
14:09Please, please, please.
14:11I was Ben Fallah.
14:12Go on to Packed Pub Publishing.
14:15Search my name up, Ben Fallah.
14:17You'll find many other of my titles.
14:19Thanks again and I hope to see you
14:20in one of our next releases together with Packed Publishing.

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!