Friday 29 May 2015

State Design Pattern Implementation

State Design Pattern


Question : - What is State Design Pattern?
This design pattern also falls under Behavioral design pattern. This pattern helps to change behavior of an object based on the object’s state. In this pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.

As per GOF:- “Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.”

Question:- How State pattern works?
Answer : -
  • We can define a context object, whose behavior varies as its state object changes.
  • We can define an object that represent various states.
Question : - When to use State design pattern?
Answer:- If we have the requirement like if we want to remember the state of the object which is mainly happening in any animated game or while making softwares for any DVD player or music players then we can proceed with this design pattern.

b) State design pattern in Java :-
  • javax.faces.lifecycle.LifeCycle#execute() (controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)
We will see an example of a new Pan Card application request. Here we will follow the below rules.
  • State changes or operation will be declared as interface. Actual implementations will have the particular/specific states with reference to the next required state.
  • State context will be responsible for instantiating the state with the initial state. 
  • When we invoke the first operation, it will keep moving to the different states for each execution till final state reaches.

State.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public interface State {
public void execute(StateContext context);
}

StateContext.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public class StateContext {
State state = null;
   public StateContext(State state){
       this.setState(new PanCardApplication());
   }
   public void setState(State state){
       this.state = state;
   }
 
   public void requestLifeCycle(){
       state.execute(this);
   }
}

PanCardApplication.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public class PanCardApplication implements State{
public void execute(StateContext context){
System.out.println("Request for new pan card accepted and acknowledgement generated!!");
context.setState(new PanCardVerification());
}
}


PanCardVerification.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public class PanCardVerification implements State{
public void execute(StateContext context){
System.out.println("Document submitted for new Pan Card are verified.");
context.setState(new PanCardAllotment());
}
}

PanCardAllotment.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public class PanCardAllotment implements State{
public void execute(StateContext context){
System.out.println("A new pan card is alloted for the given request.");
context.setState(new PanCardDelivery());
}
}


PanCardDelivery.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public class PanCardDelivery implements State{
public void execute(StateContext context){
System.out.println("A new Pan Card is delivered to the intended recipient.");
}
}


StateDesignPatternDemo.java

package com.gaurav.designpattern.state;
/**
 * @author Gaurav
 *
 */
public class StateDesignPatternDemo {
public static void main(String[] args) {
/**Creation of the state context for the request.*/
StateContext context = new StateContext(new PanCardApplication());

/**Invoking all the required operation for PAN CARD from state context. */
context.requestLifeCycle();
context.requestLifeCycle();
context.requestLifeCycle();
context.requestLifeCycle();
}
}

Result:-

Request for new pan card accepted and acknowledgement generated!!
Document submitted for new Pan Card are verified.
A new pan card is alloted for the given request.
A new Pan Card is delivered to the intended recipient.

No comments:

Post a Comment