State Design Pattern Java Example

SHARE & COMMENT :

State design pattern falls under the category of Behavioural patterns. Assume that we have an object and its behavior is largely dependent on the state of its internal variables.

State pattern allows, object to behave in different ways depends on the internal state. State pattern is defined in the book Gang of four is,

Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.

Consider the following example,

Person.java

package tips.pattern.state;
public class Person {
	private String name;
	private String character;

	public Person(String name, String character) {
		this.name = name;
		this.character = character;
	}

	public void giveMeMoney() {
		if (character.equals("GoodCharacter")) {
			System.out.println("Yes, take all my money");
		} else if (character.equals("BadCharacter")) {
			System.out.println("No, I dont have anything with me");
		} else if (character.equals("OtherCharacter")) {
			System.out.println("I will give the money tmrw");
		}
	}
}

We have modeled a Person object passing his name and a string representing his character, which may be a Good Character or Bad Character or some Other Character. If someone comes to him and asks for money by calling the Person.giveMeMoney() method, his behavior is entirely dependant on his character. Technically, his behavior is tightly coupled with the internal variable character.

state-design-patternImage Credit )
The above design has one great dis-advantage. Suppose we want to add further characters that represent some other different behavior of a Person, then we end up in adding if-else clauses, which certainly is not very good from a design perspective because of changes being done in the existing code.

So, how to provide support for different characteristic behaviors for a person without making code changes? Here comes the State pattern which just do that. The State pattern mandates to model behavior as interfaces rather than representing them as Strings or some other types. For example, since character is the changing behavior for a Person, let us encapsulate it by having something like the following,

Character.java

package tips.pattern.state;

public interface Character {
	public void giveMeMoney();
}

See, we have modeled a person’s Character as an interface. Now, let us see the implementation for different characteristic feature of a person, say GoodCharacter and BadCharacter.

GoodCharacter.java

package tips.pattern.state;

public class GoodCharacter implements Character {
	@Override
	public void giveMeMoney() {
		System.out.println("Yes, take all my money");
	}
}

BadCharacter.java

package tips.pattern.state;

public class BadCharacter implements Character {
	@Override
	public void giveMeMoney() {
		System.out.println("No, I dont have anything with me");
	}
}

Now, let us see the new Person class which accepts character in the form of an interface rather than as a String. Given below is the code snippet for the same.

Person2.java

package tips.pattern.state;

public class Person2 {
	private String name;
	private Character character;

	public Person2(String name, Character character) {
		this.name = name;
		this.character = character;
	}

	public void giveMeMoney() {
		character.giveMeMoney();
	}

	public static void main(String[] args) {
		Person2 object = new Person2("John", new GoodCharacter());
		object.giveMeMoney();
		object = new Person2("John", new BadCharacter());
		object.giveMeMoney();
	}
}

Since now the character is represented as an interface, there wont be any more code changes if there is a need to add new characteristic behavior. All we need to do is to define the new character class implementing the Character interface.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Java Rab says:

    This is not an example of State pattern, this is Strategy!

  2. I enjoyed reading your article, but I felt like the example you have shown resembles the strategy pattern more than the state pattern.

    In case of the State Pattern, the system often transitions from one state to another depending on the input. The state in which a system is determines it’s behavior.

    IMHO, perhaps adding a few things to the above example might make it closer to implementing the state pattern.

    The Person could be in one of many states:
    1. Miserly
    2. Generous
    3. Balanced

    We would have a State interface which has a method called giveMoney(). We would have 3 classes which implement the interface. One class for representing each of the above states.

    A Person is composed of it’s State. Initially a Person begins with a BalancedState, where money of given if the amount is not too large. If less money is asked for then the money is given and the person transitions to a generous state. If more money is asked for then the person transitions to Miserly state. In each state the persons response to askMoney would be different.

    This may not be the perfect example, but I feel like it will the above example closer to the state pattern.

    Do you think this would be a better example of the State Pattern ?

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

Trackbacks

  1. […] State : This patter encapsulates varying behavior for the same routine based on an object’s state object. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements. […]

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better