State Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In this article we are going to deal with a design pattern that is very similar to strategy pattern. Here we are going to learn state design pattern implementation.

State Design Pattern Implementation:

State pattern tries to implement state transitions in object oriented way. Head First Design Patterns defines it as,

It allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.

Generic Class Diagram:

State Pattern

State Pattern

Participants:

  • Context: Defines an interface to client to interact. It maintains references to concrete state object which may be used to define current state of object.
  • State: Defines interface for declaring what each concrete state should do.
  • ConcreteState: Provides implementation for methods defined in State.

 Example:

Consider we are developing a game and we have few assumptions.

  • A soldier has a life and its life has three states i.e. healthy, hurt and dead.
    • Soldier gets hurt if he gets hit by more than 5 and less than 10 bullets.
    • Solder is dead if he gets hit by more than 10 bullets.
  • If soldier is healthy, then he can fire weapons at the enemy.
  • If soldier is hurt, then he can take cover and hide somewhere and he recovers his health, he goes in healthy state again and do his duties.

Now let us design our classes based on above scenario.

  • Create interface State and declare behaviour obeyCommand(Soldier soldier), This methods needs to be implemented by all implementing classes.
  • Create classes HealthyState, HurtState and DeadState which implement State
  • Create class Soldier, which maintains references to all types of states and define method obeyCommand();

Let us get started with code.

State.java:

HealthyState.java:

HurtState.java:

Note that after soldier is saved and recovered by doing whatever needs to be done when hurt, he changes its state to healthy state. It is implemented in above code. Once he is healthy, he can obey commands for healthy state.

DeadState.java:

Soldier.java:

This maintains references to all states of soldier. current state of soldier is updated dynamically at runtime depending upon situation.

Let us send soldier on war.

Output:

Let us see what happened here and how state design pattern implementation is done.

  • First, soldier gets hit by 4 bullets, but he is strong, he doesn’t pay any heed to less than 5 bullets. He keeps doing his job when he healthy.
  • Then he gets by 7 bullets due to which he gets into HurtState and he has to take cover now. Once he recovers, he gains his health back and goes to HealthyState.
  • Notice here that from HurtState, we are changing current state of soldier to HealthyState. This is something which is different from strategy pattern. One state can remove itself and assign some other state to the context.
  • Once he gets hit by 30 bullets, nothing but darkness there, he is dead. Game Over.

Example Class Diagram:

State Pattern Example

State Pattern Example

Some points to notice in state design pattern implementation:

  • states store a reference to context object that contains them.
  • They can replace themselves, unlike strategies.
  • States can remove themselves and assign new state to object.

I hope this helps understand state design pattern implementation.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *