JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







State design pattern:
Purpose
  • Allow an object to alter its behavior when its internal state changes. The object will appear
    to change its class.
Application
  • An object's behavior depends on its state, and it must change its behavior at run-time depending on that state.
  • Operations have large, multipart conditional statements that depend on the object's state. This state is usually represented by one or more enumerated constants.
  •  Often, several operations will contain this same conditional structure.
  •  The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects.
Examples of Composite
  • The State pattern allows an object to change its behavior when its internal state changes.
  •  This pattern can be observed in a vending machine. Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion.


Example:

 
State.java
package com.model;

public interface State {
void grantPermission(StateContext stc);
void requestPermission(StateContext stc);
String getStatus();
}

AcceptedState.java
package com.model;

public class AcceptedState implements State {
  
  @Override
  public String getStatus() {
    // TODO Auto-generated method stub
    return "Request received";
  }

  @Override
  public void grantPermission(StateContext stc) {
    // TODO Auto-generated method stub
    System.out.println("Invalid state");
  }

  @Override
  public void requestPermission(StateContext stc) {
    // TODO Auto-generated method stub
    System.out.println("Requesting Permission");
    stc.setState(stc.getRequestedState());
  }

}

GrantedState.java
package com.model;

public class GrantedState implements State{
  @Override
  public String getStatus() {
    // TODO Auto-generated method stub
    
    return "Request received";
  }

  @Override
  public void grantPermission(StateContext stc) {
    // TODO Auto-generated method stub
    
    System.out.println("Invalid State");
  }

  @Override
  public void requestPermission(StateContext stc) {
    // TODO Auto-generated method stub
    System.out.println("Invalid State");
  }

}

RequestedState.java
package com.model;

public class RequestedState implements State {
  
  
  @Override
  public String getStatus() {
    // TODO Auto-generated method stub
    System.out.println("Requested Permission");
    return null;
  }

  @Override
  public void grantPermission(StateContext stc) {
    // TODO Auto-generated method stub
    System.out.println("Granting Permission");
    stc.setState(stc.getGrantedState());
  }

  @Override
  public void requestPermission(StateContext stc) {
    // TODO Auto-generated method stub
    System.out.println("Permission already requested");
  }

}

StateContext.java
package com.model;

public class StateContext {
  private State accepted;
  private State requested;
  private State granted;
  private State state;
  public StateContext() {
    super();
    // TODO Auto-generated constructor stub
    accepted=new AcceptedState();
    requested=new RequestedState();
    granted=new GrantedState();
  }
  public void acceptApplication(){
    state=accepted;
  }
  public void requestPermission(){
    state.requestPermission(this);
  }
  public void grantPermission(){
    state.grantPermission(this);
  }
  public String getStatus(){
    return state.getStatus();
  }
  public void setState(State s){
    state=s;
  }
  public State getAcceptedState(){
    return accepted;
  }
  public State getGrantedState(){
    return granted;
  }
  public State getRequestedState(){
    return requested;
  }
  
}

Test.java
package com.test;

import com.model.StateContext;

public class Test {
  public static void main(String arg[]){
    StateContext ctx=new StateContext();
    ctx.acceptApplication();
    ctx.requestPermission();
    ctx.grantPermission();
    System.out.println(ctx.getStatus());
    
  }
}

Output:-



Download Zip code

Example:-

Chain.java
package com.model;

import java.io.*;

class Chain {
    private int state;

    public Chain()     {
      state = 0
      }
    public void pull() {
        if (state == 0) {
            state = 1;
            System.out.println"   low speed" );
        else if (state == 1) {
            state = 2;
            System.out.println"   medium speed" );
        else if (state == 2) {
            state = 3;
            System.out.println"   high speed" );
        else {
            state = 0;
            System.out.println"   turning off" );
        }   
    }
  }

StateDisc.java
package com.model;

import java.io.IOException;
import java.io.InputStreamReader;
import com.model.Chain;

public class StateDisc {
    public static void mainString[] args )
throws 
IOException {
        InputStreamReader is = new
 
InputStreamReaderSystem.in );
        int ch;
        Chain chain = new Chain();
        while (true) {
            System.out.print"Press 'Enter'" );
            ch = is.read();    ch = is.read();
            chain.pull();
}   }   }

Output:-


Downlaod Zip code


Example:-


FSM.java
package com.model;

public class FSM {
     // 2. States array
     private State[] states  = new A(),
 
new B()new C() };
     
     // 3. Current state
     private int     current = 0;
     
     // 4. Delegation and pass the this pointer
     
     public void on()  { 
       states[current].on(  this )
       }
     
     public void off() {
       states[current].offthis )
       }
     
     public void ack() { 
       states[current].ackthis )
       }
     
     public void changeStateint index ) { 
       current = index; 
       }
  }

  // 5. The State base class
  abstract class State {
     // 6. Default behavior
     public void on(  FSM fsm ) {
       System.out.println"error" )
       }
     
     public void offFSM fsm ) { 
       System.out.println"error" )
     }
     public void ackFSM fsm ) { 
       System.out.println"error" )
     }
  }

  class extends State {
     public void on(  FSM fsm ) { 
       System.out.println"A + on  = C" );
        fsm.changeState)
        }
     public void offFSM fsm ) { 
       System.out.println"A + off = B" );
         fsm.changeState)}
     
     public void ackFSM fsm ) { 
       System.out.println"A + ack = A" );
         fsm.changeState)}
  }

  class extends State {
     public void on(  FSM fsm ) { 
       System.out.println"B + on  = A" );
         fsm.changeState)
         }
     public void offFSM fsm ) { 
       System.out.println"B + off = C" );
         fsm.changeState)
         }
  }

  // 7. Only override some messages
  class extends State {
     // 8. "call back to" the wrapper class
     public void onFSM fsm ) {
       System.out.println"C + on  = B" );
         fsm.changeState)
         }            
  }

StateDemo.java
package com.test;
import com.model.FSM;

public class StateDemo {
     public static void mainString[] args ) {
        FSM   fsm  = new FSM();
        int[] msgs = 2121020};
        for (int i=0; i < msgs.length; i++)
           if      (msgs[i== 0fsm.on();
           else if (msgs[i== 1fsm.off();
           else if (msgs[i== 2fsm.ack();
     }
  }

Output:-


Doqnload Zip code