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.






Wednesday, September 25

Template Design Pattern in java , Spring

                                           Template Design Pattern in java , Spring







What is Template Pattern

  – Template pattern is about defining the blueprint of the algorithm wherein some of the steps of the implemented in base class and others are deferred for subclass to implement. Spring framework implemented this pattern for many features like, JmsTemplate, JDBCTemplate.



Example:







&lt bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" scope="prototype" &gt
 &lteq bean &gt



Let's understand Template Design Pattern form core java perspective :





public abstract class ProjectManager {



public final void  collectRequirement(){

   

    System.out.println("requirements are gathered same way in all technology projects. So this could be implemented in baseclass itself and otehr classes can inherit it as it is.");

}



public  abstract void developDesign();



public  abstract void developCode();



public  final void assessQuality(){

    System.out.println("Quality is assessed in same way in all technology projects. So this could be implemented in baseclass itself and otehr classes can inherit it as it is.");

}

public  abstract void deploy();



}


ProjectManager  is abstract class. It has both abstract and implemented methods. Generic methods are implemented here while specific methods are kept abstract for implementing class to define them

Generic method are kept final So that they are not overriden and logic defined in them is used as it is in all implementations.   



  class JavaProjectManager extends ProjectManager {



    @Override

    public void developDesign() {

       

        System.out.println("Desiging is done here in JAVA way");

        // TODO Auto-generated method stub

       

    }



    @Override

    public void developCode() {

       

        System.out.println("Coding is done here in JAVA way");

        // TODO Auto-generated method stub

       

    }



    @Override

    public void deploy() {

       

        System.out.println("Deployment is done here in JAVA way");

        // TODO Auto-generated method stub

       

    }

   

   

}

JavaProjectManager   is implementation of ProjectManager class. It has defined all the specific methods as per the logic spefic to JavaProjectManager class functional requirement

 

  class MicroSoftProjectManager extends ProjectManager {



      @Override

      public void developDesign() {

         

          System.out.println("Desiging is done here in MicroSoft technology way");

          // TODO Auto-generated method stub

         

      }



      @Override

      public void developCode() {

         

          System.out.println("Coding is done here in  MicroSoft technology way");

          // TODO Auto-generated method stub

         

      }



      @Override

      public void deploy() {

         

          System.out.println("Deployment is done here in  MicroSoft technology way");

          // TODO Auto-generated method stub

         

      }

     

     

  }



-->

Similarly MicroSoftProjectManager is implementation of ProjectManager class. It has defined all the specific methods as per the logic spefic to MicroSoftProjectManager class functional requirement


 

  class mainFramesProjectManager extends ProjectManager {



      @Override

      public void developDesign() {

         

          System.out.println("Desiging is done here in mainFrames technology way");

          // TODO Auto-generated method stub

         

      }



      @Override

      public void developCode() {

         

          System.out.println("Coding is done here in  mainFrames technology way");

          // TODO Auto-generated method stub

         

      }



      @Override

      public void deploy() {

         

          System.out.println("Deployment is done here in  mainFrames technology way");

          // TODO Auto-generated method stub

         

      }

     

     

  }

Similarly mainFramesProjectManager is implementation of ProjectManager class. It has defined all the specific methods as per the logic spefic to MicroSoftProjectManager class functional requirement.

Conclusion
So template method is all about defining the execution steps in a contract , provide implementation for all generic methods in contract itself and let the implementation class implement the specific methods. But template or blueprint for all high level steps is covered in abstract class only irrespective of operation is generic or specific.