In this post, We will talk and learn about the State Design Pattern in Java.
Key Points About StateDesign Pattern :
- State design pattern falls under behavioral design pattern. This Pattern is used when an Object changes its behavior based on its internal state.
- The main benefit of using the State pattern to implement polymorphic behavior. The chances of error are very less and it’s very easy to add more states for additional behaviors. Thus making our code more robust, easily maintainable, and flexible.
Now Let’s move towards the implementation of the State Design Pattern.
Use Case:
Suppose we want to implement an AC Remote with a simple button to perform an action. If the State is ON, it will turn on the AC and if the state is OFF, it will turn off the AC.
Below is the complete source code:
State.java
1 2 3 4 5 6 7 8 9 |
package com.kkjavatutorials.state; /** * Contract to represent state * @author KK JavaTutorials */ public interface State { public abstract void doAction(); } |
ACStartState.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkjavatutorials.state.impl; import com.kkjavatutorials.state.State; /** * @author KK JavaTutorials * An Implementation to perform START/ON Action */ public class ACStartState implements State { @Override public void doAction() { System.out.println("AC is turned ON"); } } |
ACStopState.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkjavatutorials.state.impl; import com.kkjavatutorials.state.State; /** * @author KK JavaTutorials * An Implementation to perform STOP/OFF Action */ public class ACStopState implements State { @Override public void doAction() { System.out.println("AC is turned OFF"); } } |
ACContext.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kkjavatutorials.state; /** * @author KK JavaTutorials * This Contaxt class performed Action * based on state */ public class ACContext implements State { private State state; public void setState(State state) { this.state = state; } public State getState() { return state; } @Override public void doAction() { state.doAction(); } } |
ACRemoteTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kkjavatutorials.client; import com.kkjavatutorials.state.ACContext; import com.kkjavatutorials.state.State; import com.kkjavatutorials.state.impl.ACStartState; import com.kkjavatutorials.state.impl.ACStopState; /** * @author KK JavaTutorials * Client Program which makes use of * State design pattern */ public class ACRemoteTest { public static void main(String[] args) { //Create Context Object ACContext acContext = new ACContext(); //Create State Object State AcStartState = new ACStartState(); //Now setting state to start AC acContext.setState(AcStartState); //Now Perform Action acContext.doAction(); System.out.println("-------------------------------"); //Now setting State to stop AC State AcStopState = new ACStopState(); acContext.setState(AcStopState); //Now Perform Action acContext.doAction(); } } |
Output of This Program:
AC is turned ON
——————————-
AC is turned OFF
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Bridge Design Pattern in Java.
Command Design Pattern in Java
That’s all about the State Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.