Thursday, September 26

Java State Design Patten , OOPS 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.

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();

}

publicvoid setPhase(ProjectPhase phase) {

this.phase = phase;

}

public ProjectPhase getPhase() {

returnthis.phase;

}

}

class RequirementAssessment implements ProjectPhase{

publicvoid act(NextPhase nextPhase){

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

nextPhase.setPhase(new Design() );

}

}

class Design implements ProjectPhase{

publicvoid act(NextPhase nextPhase){

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

nextPhase.setPhase(new Development());

}

}

class Development implements ProjectPhase{

publicvoid act(NextPhase nextPhase){

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

nextPhase.setPhase(new QualityAssessment() );

}

}

class QualityAssessment implements ProjectPhase{

publicvoid act(NextPhase nextPhase){

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

nextPhase.setPhase(new Deploy() );

}

}

class Deploy implements ProjectPhase{

publicvoid act(NextPhase nextPhase){

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

nextPhase.setPhase(new Closure() );

}

}

class Closure implements ProjectPhase{

publicvoid act(NextPhase nextPhase){

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

}

}

publicclass ProjectExpeditor {

/**

* @param args

*/

publicstaticvoid 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....

Subscribe via email

About Me

Mobile NO : 7503454801
Email address : manojbharal2@gmail.com
About Blog
Blog consist of posts on java j2ee technical topics. It covers all range of post starting with basic java questions up to advanced j2ee architecture. I am owning this blog. Content being published here is authentic and original . This is growing blog and visitors can expect huge relevant and useful information to enhance their knowledge on subject.
About me
I have total of around 8 years of experience in java j2ee technologies and am working on multiple business domain technical projects in all range of industries .