- Context: Acts as the interface to clients. The Context object maintains an instance of a ConcreteState subclass that represents the current state. The context delegates the requests to the concrete state object that represents the current state. Typically, the context passes itself as an argument to the current state object, which changes the current state of the context.
public class StateContext {
private State acceptedState;
private State requestedState;
private State grantedState;
private State state;
public StateContext() {
acceptedState = new AcceptedState();
requestedState = new RequestedState();
grantedState = new GrantedState();
state = null;
}
public void acceptApplication() {
this.state = acceptedState;
}
public void requestPermission() {
state.requestPermission(this);
}
public void grantPermission() {
state.grantPermission(this);
}
public String getStatus() {
return state.getStatus();
}
public void setState(State state) {
this.state = state;
}
public State getAcceptedState() {
return acceptedState;
}
public State getGrantedState() {
return grantedState;
}
public State getRequestedState() {
return requestedState;
}
}StateContext.java - State: An interface for encapsulating the behavior associated with a particular state of the Context.
public interface State {
public void grantPermission(StateContext ctx);
public void requestPermission(StateContext ctx);
public String getStatus();
}State.java - Concrete State subclasses: Define the behaviour associated each state.
public class RequestedState implements State {
public void grantPermission(StateContext ctx) {
System.out.println("Granting Permission");
ctx.setState(ctx.getGrantedState());
}
public void requestPermission(StateContext ctx){
System.out.println("Permission already requested");
}
public String getStatus() {
return "Requested permission";
}
}RequestedState.java public class AcceptedState implements State {
public void grantPermission(StateContext ctx) {
}
public void requestPermission(StateContext ctx){
System.out.println("Requesting permission");
ctx.setState(ctx.getRequestedState());
}
public String getStatus() {
return "Request Received";
}
}AcceptedState.java public class GrantedState implements State {
public void grantPermission(StateContext ctx) {
System.out.println("Invalid state");
}
public void requestPermission(StateContext ctx){
System.out.println("Invalid state");
}
public String getStatus() {
return "Granted";
}
}GrantedState.java - Client
public class StateClient {
public static void main(String[]args) {
StateContext ctx = new StateContext();
ctx.acceptApplication();
ctx.requestPermission();
ctx.grantPermission();
System.out.println(ctx.getStatus());
}
}StateClient.java
Monday, December 18, 2006
Implementing State Pattern in Java
In the previous post, I described a little about the State design pattern and it's relation to the Strategy pattern. In this post, I will show how to implement the State Pattern in Java. The following is the UML diagram for the State Pattern. A description and sample code for the example follows:
Subscribe to:
Post Comments (Atom)
Popular Posts
-
In a previous post, I described how to use Quartz scheduler for scheduling . In this post, I describe the configuration changes required for...
-
In the previous post, I described different ways in which spring and hibernate can be integrated. In this post I will describe how to use Sp...
-
The previous post described how to implement a JMS messaging client using Spring JMS . This post will describe how to implement the Message ...
-
JUnit 4 introduces a completely different API to the older versions. JUnit 4 uses Java 5 annotations to describe tests instead of using in...
-
In the previous post, I described the use of Displaytag to implement paging in a simple JSP. In this example, I describe the use of Displayt...
-
In this post we will see how to do an offline install Jenkins and required plugins on a Red Hat Enterprise Linux Server release 7.3. This is...
-
New posts with iText 5.5.12 Following are two new posts for PDF Merge with iText 5.5.12 Merge PDF files using iText 5 Merge and Paginate PDF...
-
This post will describe how to create and deploy a Java Web Application war to Heroku using Heroku CLI. You will need a basic understanding ...
-
In this post we will see a way to merge multiple PDF files while adding page numbers at the bottom of each page in the format Page 1 of 10 ....
-
Displaytag is an opensource tag library that can be used to display tables on JSPs. Apart from being able to display tables, the displaytag...
I like the code and have learned a lot from it. But, this code is a bit against the open close principle. If I have another state to be added other than the stated ones, then I should implement on all classes. I think this is not a good approach. Other than that I have used it for my understanding thankyou.
ReplyDeleteI really apreciate this blog, your explanations are quite clear. But I'm still wondering when use patters, I mean if I have a class, for instance Book, and I need to keep the state of the book, I mean In Stock, etc. I can have a property with the state, or I can implement the state pattern, which solution is better, how I can determine the proper one.....
ReplyDeleteGreat work, simple & clear, thx!
ReplyDeleteState 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.
ReplyDeleteFor example In JSF framework entire web request response lifecycle is divided in six phases:
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.
Learn more about State design pattern here -- State Design Pattern
http://efectivejava.blogspot.in/2013/09/java-state-design-patten-oops-state.html?utm_source=BP_recent
Investing Secne Is A Webportal For Traders To Find The Best Brokers And Strategies Online On A Single Platform. With Constant Reviews, Accessible Guest Posts, Blogs And More, We Help Them Find What They're Looking For. We Have Industry News And Price Quotes In One Place That May Affect Investment Decisions.
ReplyDelete