State Design Pattern

What is state design pattern?

State design pattern lets an object alter its behavior when its internal state changes. It is a behavioral design pattern.

Advantages of state design pattern:

  • State design pattern minimizes conditional complexity.
  • It eliminates the need for if and switches statements on objects with different behavioral requirements

Disadvantages of state design pattern:

  • The implementation of State design pattern requires a lot of code to be written.
  • It increases complexity for simple use cases

Difference between Strategy vs State design pattern

StateStrategy
Different states can be dependent on each otherStrategies are independent and not aware of each other
The state design pattern is about doing different things based on the state, hence the result may varyStrategy design pattern is about having different implementations that accomplish the same thing

When to use state design pattern:

When an object has a limited number of possible states and its behavior depends on its current state. For example, a traffic light object can be in one of three states: red, yellow, or green. The behavior of the traffic light, such as whether it allows cars to pass or not, depends on its current state.
When you want to avoid complex conditional logic in your code. The State pattern can help you to decouple the object’s behavior from its internal state, making it easier to maintain and extend the code.
When you want to encapsulate state-specific behavior. The State pattern can help you to encapsulate state-specific behavior into separate classes, making it easier to reuse and test the code.

Example usage of State design pattern:

The Java javax.swing.JButton class uses the State pattern to model the different states of a button (normal, pressed, and disabled). The behavior of the button, such as whether it is highlighted or clickable, depends on its current state.
The Java javax.swing.JFileChooser class uses the State pattern to model the different states of a file chooser (open, save, or multi-select). The behavior of the file chooser, such as whether it allows users to select files or folders, depends on its current state.

Code Example of State design pattern:

// state.ts
export interface State {
    handle(context: OrderState): void;
}

export class startOrder implements State {
    public handle(context: OrderState): void {
        console.log("`handle` method of startOrder is being called!");
    }
}

export class finishOrder implements State {
    public handle(context: OrderState): void {
        console.log("`handle` method of finishOrder is being called!");
    }
}

export class OrderState {
    private state: State;

    constructor(state: State) {
        this.state = state;
    }

    get State(): State {
        return this.state;
    }

    set State(state: State) {
        this.state = state;
    }

    public request(): void {
        console.log("request is being called!");
        this.state.handle(this);
    }
}

// client.ts
import { OrderState, startOrder, finishOrder } from './state'


let context = new OrderState(new startOrder());
context.request();

context = new OrderState(new finishOrder());
context.request();
$ node client.js
request is being called!
`handle` method of startOrder is being called!
request is being called!
`handle` method of finishOrder is being called!
Reference