Setting up Eclipse and Green for our example
      To begin, you need to download the source code for the State Pattern example. The location will be provided by Dr. Alphonce. Right-click in the "Package Explorer" view and select "Import...". Select "Existing Projects into Workspace" and click "Next", then choose " Select archive file" and use "Browse" to locate the archive. Select "Finish" in the "Import" window when you've found the archive.
      Find the file "TrafficLight.dia" and double-click it to open the diagram in the editor.
State Design Pattern
      The best way to describe the state design pattern is by describing a behavior we all (should) know. A simple traffic light is shown to the left. In it, we see two lamps, one on top (Red, or STOP) and one on the bottom (Green, or GO.) The light thus has two separate "states": a "red" state and a "green" state. Determination of which state comes next will be delegated to the states.

How state works
      The "State" design pattern relies on an arbitary number of objects that represent the states our main object can have. In our case, the main object is called "TrafficLight". Our states will share a common interface, which we will call "ILampState". That interface will define the methods that our states will have in common.

      One behavior a traffic light is responsible for is displaying the current color of the light. Another is the ability to change from one color to another. Double-click on the getColor() method in the diagram inside the "TrafficLight" box. Change the code inside that method to match the following:

public Color getColor() {
     return _myState.getColor();
}

      Notice that the method refers to the variable "_myState", which will be the class' "current state". The TrafficLight class delegates to its states to return what "state" it is currently in. Since all the states share a common interface (ILampState), the method getColor() is guaranteed to be present for all of the implementations of the states. Close the code window.
      Right-click on TrafficLight, go to Add -> AddField. For the name, use _myState. For the type, use ILampState.
      Select the "Interface" tool in the tool pane, then click anywhere on the white space in the UML diagram area to create the interface. Name it "ILampState" and press "Finish".
      Now our diagram has one class (TrafficLight) and one interface for all states to implement (ILampState). We now need to begin defining the methods which our Lamp States must implement. Ask yourself the following: "What does a lamp state need to do?"
      As we illustrated at the beginning of our tutorial, a lamp state must inform us what state it is in. It has other responsibilities, but at first, we'll just focus on getting the lamp's current color
      Recall that we call a method called getColor() that belongs to ILampState. Since we're defining the method in the interface first, it doesn't need to have a body. When we have concrete implementation of the interface, we will define how the "getColor()" method will actually get the color. Interfaces provide a set of guaranteed methods that concrete classes will have.
      First, we have to add the method to the interface. Right-click on the interface "ILampState" in the diagram and select Add -> Add Method. A dialog will appear. Name the method getColor. In the Type field, type "java.awt.Color" and click "Finish". Double-click the interface class to look at the code inside. It should look similar to the following:

public interface ILampState {
     public java.awt.Color getColor();
}

      Close the code window. What this method means is that anything that is an ILampState must have the ability to do getColor. This method must be defined by our concrete classes. We may be able to see what state we're in by querying the color, but how do we change states? As mentioned before, we have two transitions: Red changes to Green; Green changes to Red. To do this in code, we need another method that will tell us what state to go to next. Follow the steps above to add another method to the interface, except this time you should call the method getNextState and the type ILampState.
      The next step is to provide a way to change from the current state to the next state. Double-click the TrafficLight box. A code window should open up. Change the changeColor method's code to look like the following:

public void changeColor() {
     _myState = _myState.getNextState();
}

      Close the code window. It's time to begin "realizing" our interface into concrete classes that implement our interface. To do this, we first declare the classes that will represent the states. They will be called RedState and GreenState. Create a class for each one in the diagram.
      We'll use Green to cause the classes to implement ILampState. Select the realization relationship tool from the palette. Realization is a special UML relationship that we use when a class object "implements" an interface. After selecting the realization tool, click on RedState and then ILampState. A relationship arc should appear. You'll notice that there's a problem indicated by a red X in the top left corner of the RedState box.

      The selection tool should now be active; after drawing a relationship, the palette automatically reverts to the default tool after drawing is performed. Double-click on the RedState class to open up the code window. There should be a symbol near the words "public class RedState implements". Click on the error symbol; a small box should appear with a couple options to fix the problem. Select Add unimplemented methods. Eclipse will generate method stubs for you, but since it can't possibly know what the methods are supposed to do, the implementation will do nothing. Double-click on the RedState class to open up the code editor

Inside the getColor method, put:
return java.awt.Color.red;

Inside the getNextState method, put:
return new GreenState();

      Close the code window. You'll want to repeat that entire procedure for the GreenState class, substituting java.awt.Color.green and RedState where appropriate.
      We need to set an initial state for our traffic light. Open up the TrafficLight class and change the code in the constructor to match the following. This code will be executed when a new TrafficLight is created:

public TrafficLight() {
     _myState = new RedState();
}

      In the "Package Explorer" view, click on Runner.java and select Run -> Run As -> Java Application from the menu at the top. A window should appear with a red box. Click on the box; you should observe it change from red to green. If you click again, it will go from green to red. If you're feeling ambitious, try adding a yellow light in.