tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Design Patterns > Java Design Patterns > State : Project Life Cycle Example

State : Project Life Cycle Example 

State pattern comes under Behavioral design pattern. It lets an object to change its behavior when its internal state changes.

Behaviour & Advantages

  • Supports state specific behavior.
  • Defines an object oriented state machine.
  • The state context hold a reference to current state.
Participants
  • State Context
    Maintains a reference to current state which is initialized with an initial state. It lets the clients change its state by exposing some API.
  • State
    Abstract interface for defining state specific behavior.
  • Concrete State
    An implementation of State specific behavior.

This examples shows project lifecycle in waterfall model. Here ProjectLifeCycle class acts as State Context and IProjectState interface acts as State. Life cycle phases such as RequirementAnalysis, Design, Implementation, Testing, Deployment and Maintenance acts as Concrete States. ProjectLifeCycle is instantiated with an initial state RequirementAnalysis.

Project State interface is shown below,

File Name  :  
com/bethecoder/tutorials/dp/state/IProjectState.java 
   
package com.bethecoder.tutorials.dp.state;

public interface IProjectState {

  /**
   * Move the state of project life cycle to next state.
   
   @param projectLifeCycle
   */
  public void nextPhase(ProjectLifeCycle projectLifeCycle);
  
}
   

Project Life Cycle class is shown below,

File Name  :  
com/bethecoder/tutorials/dp/state/ProjectLifeCycle.java 
   
package com.bethecoder.tutorials.dp.state;

import com.bethecoder.tutorials.dp.state.phases.RequirementAnalysis;

public class ProjectLifeCycle {

  private IProjectState curProjectState = null;
  
  public ProjectLifeCycle() {
    curProjectState = new RequirementAnalysis()
  }

  public IProjectState getCurProjectState() {
    return curProjectState;
  }

  public void setCurProjectState(IProjectState curProjectState) {
    this.curProjectState = curProjectState;
  }
  
  public void nextPhase() {
    curProjectState.nextPhase(this);
  }
  
  public String getCurrentPhase() {
    return curProjectState.getClass().getSimpleName();
  }
}
   

Requirement Analysis Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state/phases/RequirementAnalysis.java 
   
package com.bethecoder.tutorials.dp.state.phases;

import com.bethecoder.tutorials.dp.state.IProjectState;
import com.bethecoder.tutorials.dp.state.ProjectLifeCycle;

public class RequirementAnalysis implements IProjectState {

  @Override
  public void nextPhase(ProjectLifeCycle projectLifeCycle) {
    System.out.println("Project requirement analysis has been completed");
    projectLifeCycle.setCurProjectState(new Design());
  }
}
   

Design Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state/phases/Design.java 
   
package com.bethecoder.tutorials.dp.state.phases;

import com.bethecoder.tutorials.dp.state.IProjectState;
import com.bethecoder.tutorials.dp.state.ProjectLifeCycle;

public class Design implements IProjectState {

  @Override
  public void nextPhase(ProjectLifeCycle projectLifeCycle) {
    System.out.println("Project design has been completed");
    projectLifeCycle.setCurProjectState(new Implementation());
  }
}
   

Implementation Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state/phases/Implementation.java 
   
package com.bethecoder.tutorials.dp.state.phases;

import com.bethecoder.tutorials.dp.state.IProjectState;
import com.bethecoder.tutorials.dp.state.ProjectLifeCycle;

public class Implementation implements IProjectState {

  @Override
  public void nextPhase(ProjectLifeCycle projectLifeCycle) {
    System.out.println("Project implementation has been completed");
    projectLifeCycle.setCurProjectState(new Testing());
  }
}
   

Testing Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state/phases/Testing.java 
   
package com.bethecoder.tutorials.dp.state.phases;

import com.bethecoder.tutorials.dp.state.IProjectState;
import com.bethecoder.tutorials.dp.state.ProjectLifeCycle;

public class Testing implements IProjectState {

  @Override
  public void nextPhase(ProjectLifeCycle projectLifeCycle) {
    System.out.println("Project testing has been completed");
    projectLifeCycle.setCurProjectState(new Deployment());
  }
}
   

Deployment Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state/phases/Deployment.java 
   
package com.bethecoder.tutorials.dp.state.phases;

import com.bethecoder.tutorials.dp.state.IProjectState;
import com.bethecoder.tutorials.dp.state.ProjectLifeCycle;

public class Deployment implements IProjectState {

  @Override
  public void nextPhase(ProjectLifeCycle projectLifeCycle) {
    System.out.println("Project deployment has been completed");
    projectLifeCycle.setCurProjectState(new Maintenance());
  }
}
   

Maintenance Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state/phases/Maintenance.java 
   
package com.bethecoder.tutorials.dp.state.phases;

import com.bethecoder.tutorials.dp.state.IProjectState;
import com.bethecoder.tutorials.dp.state.ProjectLifeCycle;

public class Maintenance implements IProjectState {

  @Override
  public void nextPhase(ProjectLifeCycle projectLifeCycle) {
    System.out.println("Project maintenance is in progress ...");
    projectLifeCycle.setCurProjectState(new Maintenance());
  }
}
   

This example accepts an input from the user and moves the project life cycle to its next phase. State pattern usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/state/Test.java 
   
package com.bethecoder.tutorials.dp.state;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {

    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    String line;
    
    ProjectLifeCycle projectLifeCycle = new ProjectLifeCycle();
    System.out.println("----Project Life Cycle Management----");
    
    while (true) {
      System.out.println("Current Phase : " + projectLifeCycle.getCurrentPhase());
      
      line = console.readLine();
      if ("exit".equalsIgnoreCase(line.trim())) {
        break;
      }
      
      projectLifeCycle.nextPhase();
    }
  }

}
   

It gives the following output,
----Project Life Cycle Management----
Current Phase : RequirementAnalysis

Project requirement analysis has been completed
Current Phase : Design

Project design has been completed
Current Phase : Implementation

Project implementation has been completed
Current Phase : Testing

Project testing has been completed
Current Phase : Deployment

Project deployment has been completed
Current Phase : Maintenance

Project maintenance is in progress ...
Current Phase : Maintenance

Project maintenance is in progress ...
Current Phase : Maintenance

Project maintenance is in progress ...
Current Phase : Maintenance

Project maintenance is in progress ...
Current Phase : Maintenance



 
  


  
bl  br