State Pattern Recipe

1.)  The basic idea of the state pattern is that you want to create an interface or an abstract class  that encapsulates several methods that have different functionality depending upon the what state the program is in.

2.)  The first thing you will want to do is draw a state diagram to figure out what possible states your program can enter and then encapsulate what varies with code.

3.) The interface or abstract class that encapsulates your methods will be the blueprint for your different types of states so you should name it state respectively.

4.) Within you state interface or abstract class you will want to set up the blueprint for how your actual states should be put together in the constructor.  Basically what you want to do is pass in an instance of whatever portion of your program that actually has the different types of states within the constructor of state.

5.)  Next you want to build the specific types of states that your program can enter.  Have your states implement or extend your state interface or abstract class respectively.   Then simply hard code the functions that you setup in your state class and construct the states with super() and your reference to the portion of your program that can enter all the different states.

6.) Last but not least you must create instances of both your state parent class and all the different state children in the portion of your program that can enter the different states and instantiate them.   You create a state variable to store states in and set its initial state.  You create instances of your different state children so that you may change the state at anytime by simply copying it into your main state variable.  Also now instead of having to worry about all that code that you put into your different state functions now all you have to do is simply say state.functionName() within your functions and it delegates the responsibility onto your state class and it delegates down to its state children.


One Response to “State Pattern Recipe”

  1. Nicholas Says:

    Looks OKish. This one is very vague, much more so thatn your other posts. You are missing the part that says how you use it in your project. Note however, that you do not need to create instances of a state the object is not in. The state object can create a new instance of the next state when it is time to change. This is tied to one of the big parts of the state pattern idea, states change themselves. The state has a method called, say moveAlong(), and somewhere in that method it realizes that the object that it is a state of should now be in a different state, (perhaps the state noticed that the object has seen a predator). So the state object makes a new state object of a different state class (say a running away state instead of a grazing state), and then changes the currentState variable in the object that it is a state of, so the next time that object calls currentState.moveAlong() it gets the method in the state for fleeing frantically from predators.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: