Home » Java » Core Java » Design Patterns: State

About Alexey Zvolinskiy

Alexey Zvolinskiy
Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Design Patterns: State

This article will be about State design pattern. It is one of behavioral design patterns. You don’t need to know many theory to understand the main concepts of the pattern. The post will be break in several parts where I will provide information about situations where the pattern need to be applied, cons and pros which it has and an example of usage.java-design-patterns

Sometimes you need to change a behavior of object when its internal state changes. The State design pattern allows to do this. You can obtain this by creation of separate classes which represent different states and functionality. Of Course these classes have to be inherited from one abstract class or implement one interface.

The State design pattern can be used when we need to change state of object at runtime by inputting in it different subclasses of some State base class. This circumstance is advantage and disadvantage in the same time, because we have a clear separate State classes with some logic and from the other hand the number of classes grows up.

Let’s consider the example. Each year has 4 seasons: Winter, Spring, Summer and Autumn.Each season has its own order, e.g. Spring comes after Winter, Summer comes after Spring and so on.

Base state interface for the seasons:

public interface Season {
	public void theSeason(SeasonContext context);


State classes which implement the Season interface:

public class Winter implements Season {

	public void theSeason(SeasonContext context) {
		System.out.println("Winter is now.");
		context.setSeason(new Spring());


Spring class:

public class Spring implements Season {

	public void theSeason(SeasonContext context) {
		System.out.println("Spring is now");
		context.setSeason(new Summer());


I’ll omit classes for Summer and Autumn, because they are the same as previous classes.

The SeasonContext class:

public class SeasonContext {
	private Season season;

	public SeasonContext() {
		this.season = new Winter();

	public void setSeason(Season season) {
		this.season = season;

	public void whatTheSaeson() {

All this stuff shows architecture of the State pattern: base state interface, classes which implement the state interface and the state context. Now let’s see how it works:

	public static void main(String[] args) {
		SeasonContext sc = new SeasonContext();

The result will be:

Winter is now.
Spting is now
Summer is now.
Autumn is now.


Reference: Design Patterns: State from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Java code desiging
8 years ago

Really informative post .
Composite design pattern is based on creating a tree structure in such a way that an individual leaf of the tree can be treated just like entire tree composition.

8 years ago

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: 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… Read more »