STATE PATTERN

State design pattern is used when an Object change its behavior when it’s internal state changes.

The state of an object can be defined as its exact condition at any given point of time, depending on the values of its properties or attributes. The set of methods implemented by a class constitutes the behavior of its instances. Whenever there is a change in the values of its attributes, we say that the state of an object has changed.

When a Context object is first created, it initializes itself with its initial State object. This State object becomes the current State object for the context. By replacing the current State object with a new State object, the context transitions to a new state. When an application object makes a call to a Context method (behavior), it forwards the method call to its current State object.

Context: Defines the interface of interest to clients. Maintains an instance of a ConcreteState subclass that defines the current state.

State: Defines an interface for encapsulating the behavior associated with a particular state of the Context.

ConcreteState subclasses: Each subclass implements a behavior associated with a state of the Context.

In the state pattern, we have a Context class, and this class has a State reference to a Concrete State instance. The State interface declares particular methods that represent the behaviors of a particular state. Concrete States implement these behaviors. By changing a Context’s Concrete State, we change its behavior. In essence, in the state pattern, a class (the Context) is supposed to behave like different classes depending on its state. The state pattern avoids the use of switch and if statements to change behavior.

Example:

package com.myjavablog.behavioural.state;

public interface MobileState {

public void getState();
}

package com.myjavablog.behavioural.state;

public class Ringing implements MobileState {
@Override
public void getState() {
System.out.println("Mobile in Ringing state");
}
}

package com.myjavablog.behavioural.state;

public class Vibrate implements MobileState {
@Override
public void getState() {
System.out.println("Mobile in Vibrate mode");
}
}

package com.myjavablog.behavioural.state;

public class Silent implements MobileState {
@Override
public void getState() {
System.out.println("Mobile in Silent mode");
}
}

package com.myjavablog.behavioural.state;

public class MobileContext implements MobileState {

private MobileState mobileState;

public MobileContext(MobileState mobileState) {
this.mobileState = mobileState;
}

public void setMobileState(MobileState mobileState) {
this.mobileState = mobileState;
}

@Override
public void getState()
{
mobileState.getState();
}
}

package com.myjavablog.behavioural.state;

public class StatePatternTest {

public static void main(String[] args) {

MobileContext context = new MobileContext(new Ringing());
context.getState();
context.getState();
context.setMobileState(new Vibrate());
context.getState();
context.getState();
context.getState();
context.setMobileState(new Silent());
context.getState();
context.getState();

}
}

Output:

Mobile in Ringing state
Mobile in Ringing state
Mobile in Vibrate mode
Mobile in Vibrate mode
Mobile in Vibrate mode
Mobile in Silent mode
Mobile in Silent mode

The benefits of using State pattern to implement polymorphic behavior is clearly visible, the chances of error are less and it’s very easy to add more states for additional behavior making it more robust, easily maintainable and flexible. Also State pattern helped in avoiding if-else or switch-case conditional logic in this scenario.

When to use the State Design Pattern:

Use the State pattern in either of the following cases:

  • An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state.
  • Operations have large, multipart conditional statements that depend on the object’s state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object’s state as an object in its own right that can vary independently from other objects.

State Design Pattern in Java:

  • javax.faces.lifecycle.LifeCycle#execute ()

Leave a Comment

Bitnami