Thursday, September 26

Java State Design Patten , OOPS state design pattern

                                         Java State Design Patten , OOPS state design pattern







State Design Pattern 
 
State design pattern works on the concept of state change. Entire process life-cycle can be divided in multiple phases.With completion of each phase process exits from one state and enters in to another state. 

For example In JSF framework entire web request response lifecycle is divided in six phases:

1. RestoreViewPhase
2. ApplyRequestValuePhase
3. ProcessValidationPhase
4. UpdateModelvaluesPhase
5. InvokeApplicationPhase
6. renderResponsePhase


After completion of every phase process exits from a state and enters into another state. For example after RestoreValuePhase we can say ViewRestored as exit state and RequestApply as entering state . 

So to implement State design pattern It is required to divide entire process in such a way that It can be processed in multiple phases with every phase exit defining a state change. 

Now let's understand this with below code. 


Any project lifecycle can be divided in multiple phases like 


  • requirementAssessment
  • Design
  • Development
  • QualityAssessment
  • Deploy
  • Closure


So these are the phases used in below example 

Rules : 

1. We need to define a class where we can store the current state of the process. NextPhase class in below code is doing that.

2. We need to define an Interface wherein we can provide the contact method that would be implemented in each phase.In below code ProjectPhase is doing that.

3. For each phase we need to write a class implementing ProjectPhase interface. 

4. ProjectExpeditor is client class which basically utilize the design pattern features.


 Describing below Example
 When NextPhase instance is created in Client (ProjectExpeditor ) class it will set the initial state of the process. In below example first state is RequirementAssessment.
Then client code continues retrieving the Next phase and execute the common method on that. 
With every execution current state is changed to next state . This responsibility lies on implementing classes. In below code After business logic execution every class in calling setPhase() method of NextPhase class and sets the Next state. So next invocation from client would be on next state instance .

Client code will set an exit condition ,which is nothing but last phase of process , Closure phase in this example 



-->



   interface ProjectPhase{

   

      void act(NextPhase nextPhase);

   

   }

 

class NextPhase {



    ProjectPhase phase;



    public NextPhase() {

        this.phase = new RequirementAssessment();

    }



    public void setPhase(ProjectPhase phase) {

        this.phase = phase;

    }



    public ProjectPhase getPhase() {

       return this.phase;

    }

}

 

  class RequirementAssessment implements ProjectPhase{

     

      public void act(NextPhase nextPhase){

         

          System.out.println("requirement analysis is done......");

         

          nextPhase.setPhase(new Design() );

      }



  



  

  }

 

class Design implements ProjectPhase{

     

      public void act(NextPhase nextPhase){

         

          System.out.println(" Design is done......");

         

          nextPhase.setPhase(new Development()  );

      }



  



  

  }

class Development implements ProjectPhase{

   

    public void act(NextPhase nextPhase){

       

        System.out.println("Development is done......");

       

        nextPhase.setPhase(new QualityAssessment() );

    }









}

class QualityAssessment implements ProjectPhase{

   

    public void act(NextPhase nextPhase){

       

        System.out.println("Quality assessment is done......");

       

        nextPhase.setPhase(new Deploy() );

    }









}

class Deploy implements ProjectPhase{

   

    public void act(NextPhase nextPhase){

       

        System.out.println("Deployment is done......");

        

        nextPhase.setPhase(new Closure() );

    }





 }



class Closure implements ProjectPhase{

   

    public void act(NextPhase nextPhase){

       

        System.out.println("Project is closed....");

       

  }





 }


public class ProjectExpeditor {



    /**

     * @param args

     */

    public static void main(String[] args) {

       

        NextPhase nextPhase=new NextPhase();

        ProjectPhase projectPhase=nextPhase.getPhase();

      

        while(true){


         if(projectPhase instanceof Closure){

             break;

         }else{

             projectPhase=nextPhase.getPhase();

             projectPhase.act(nextPhase);

         }

        }

  

    }

   

  }




Executing above code will result :

 requirement analysis is done......
 Design is done......
Development is done......
Quality assessment is done......
Deployment is done......
Project is closed....


You can directly copy and execute this code to understand further.






No comments:

Post a Comment

Post a Comment