Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5

How the State pattern works
This diagram shows that the PowerBand class is an abstract class and the Car object contains a PowerBand object. The PowerBand class really acts as a kind of placeholder. Because it is abstract, it is not possible to create a PowerBand variable. Instead,
the variable will hold one of the objects created from a subclass of PowerBand.
Each subclass then defines the behavior appropriate to that state. When the Car object receives a request to accelerate, it delegates the request to its state object. The kind of acceleration you get depends
on the state the car is in at the time.
Here is the code from State.java, stripped of its comments. In this section, we'll analyze this code to see how it works.
abstract class State {
static State initialState;
static final State state1 = new State1();
static final State state2 = new State2();
protected State() {
if (initialState == null) initialState = this;
}
abstract void stateExit(StateOwner owner);
abstract void stateEnter(StateOwner owner);
}
class State1 extends State {
void stateExit(StateOwner owner) {
}
void stateEnter(StateOwner owner) {
}
}
class State2 extends State {
void stateExit(StateOwner owner) {
}
void stateEnter(StateOwner owner) {
}
}
Analysis
To understand this bit of code, you'll need to take it apart and look at it, piece by piece.
State classes
Taking a high-level overview, you can see three classes:
abstract class State {
...
}
class State1 extends State {
...
}
class State2 extends State {
...
}
The State class is an abstract class. It provides some basic behavior, but its only real purpose is to be extended to produce one or
more "real" state classes. In this case, two state classes are defined, State1 and State2. To create additional states, you would copy one of these classes.
Static variables
The State class defines three variables:
abstract class State {
static State initialState;
static final State state1 = new State1();
static final State state2 = new State2();
...
The first variable is the default initial state. The first state that gets created will be stored in that variable, in case
the StateOwner object wants to use it. (It doesn't have to.) The next two variables create the state objects using the classes State1 and State2.
Because these variables are defined as static, they belong to the State class. In other words, they are class variables, which means they belong to the class as a whole, rather than to individual objects. Without the static keyword, they would be instance variables, meaning that each object created using the class (each instance of the class) would have its own variable.
In general, using the static keyword means there is only one copy of the variable for the entire class, no matter how many objects are created using that
class. Such variables generally are used for class-wide data, such as a count of the number of objects that are created using
the class.
Protected constructors
The next significant part of the State class is the constructor, which saves the first state created as the default initial state. The important feature of the
constructor is that it is defined as protected:
protected State() {
if (initialState == null) initialState = this;
}
Because the constructor is protected, it cannot be used by other classes to create new objects. It can only be used by the
State class and by those classes that extend the State class.
Since the State class is abstract, only the subclasses have access to the constructor, which is equivalent to making the subclass constructors
private. In other words, the subclasses can create themselves using static variables, but they can never be used at run time to create
additional objects.
In this case, the abstract State class creates the static variables state1 and state2. The protected constructor forms a "closed loop" with these static variables. The creation of the static variable state1 accesses the State1 constructor, which invokes the State constructor, which is protected. The final result is that the state
variables can only be created from within the State class and its subclasses. They form a predetermined set that is defined at compile time.
Abstract methods
The last part of the State class defines two abstract methods, stateExit() and stateEnter():
...
abstract void stateExit(StateOwner owner);
abstract void stateEnter(StateOwner owner);
}
Because these methods are defined as abstract, they have no method body. That means they must be implemented by the subclasses that extend the State class. That, fundamentally, is the reason for defining an abstract class in the first place -- to specify the kinds of methods
the individual states will provide, and leave the implementation up to them.
The State class says that each state will have two methods, one for when you exit (or leave) a state, and one for when you enter (or
arrive at) a state. Although you do not always need both methods, frequently it is helpful to have them. In multiple state
systems, the combination of leaving one state and arriving at another can completely define the actions needed for a transition
between the two states.
For example, consider the three states Accelerating, Slowing Down, and Stopping, as shown in the following diagram: