Monday 28 April 2025

Implementing State Pattern in Java

Bookmark and Share
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:State Pattern UML
  • 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

{ 0 comments... Views All / Send Comment! }

Post a Comment