Monday, 25 June 2012

State Pattern

What's the State Pattern?

In the Design Pattern:
Allow an object to alter its behavior when its internal state changes. The will appear to change to its class.



File:State Design Pattern UML Class Diagram.svg 



From this UML image, all the request() function calls the state.handle(), the Context don't care the which state is, just call the state.handle(). IF you not use the state pattern, you maybe implement the request() like

     switch():
           case ConcreteStateA:
                  dosomething();
           case ConcreteStateB:
                  dosomething();
when have more than one functions which implement must based on state choose, so I have the many switch case, one day you want to remove one state or add one state, you must modify all the functions which has this state choose. That is very crazy.

For example, the Mars Rovers problem:
the mars rover at the mars surface(like Cartesian Coordinate(x,y)), it has two the actions, turn left and move, and the four direction it faced, south , north, east, west.

so image the original place is (1, 5, N) means the mars rover at
coordinate (1, 5), face the North direction.
when we give the turn left command, it will become (1, 5, W), (very useful Hint from my geography teacher, In the map, up->north, down->south, left->west, right->east ).

when we give the move command, it will become (0, 5, W), the x -1, move the negative direction of x.

So You maybe have the idea for it about state pattern.

Class Rover
{
        State s;
        int x;
        int y;
        turnleft(Rover r){
             s.handleturnleft(this);     // the turnleft function call state handleturnleft function.
        }
        move(Rover r){
              s.handlemove(this);      // the move function call state handlemove function.
         }
        setState(State s){
               this.s = s;
         }
}

interface State
{
       handleturnleft(Rover r);

      handlemove(Rover r);
}

and we have the four the state inherit from State.   Give the NorthState implemention.

class NorthState : State
{
       handleturnleft(Rover r){
               r.setState(new WestState());
       }

      handlemove(Rover r){
             r. y++
      };

}


Mar Rover at (1, 5, N), and send the turn left command what happend?


the Rover->turnleft().

and the Rover's state change from NorthState to WestState.

We don't have the State choose at turnleft(, these hide in the state class,  the NorthState
knows the it turn left, will become the WestState, and It move, make the Rover.y + 1. We have no conditional choose in the function, it's very distinct.

You maybe know where use the State pattern

1.  An object's behavior depends on its state, and it must change its behavior run-time depending on that state.

2.  Operations have large, multiparty conditional statements that depend on the object's state.

The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's the object's state as an object in its own right that vary independently from other objects. 

State pattern puts all behavior associated with a particular state into one object. Because all state specific code lives in a State subclass, new states and transitions can be added easily by defined new subclass. The State class is more easy to share between other classes.


Reference links:

1. Design Pattern Book (GOF)
2. http://en.wikipedia.org/wiki/State_pattern









No comments:

Post a Comment