What is state design pattern?

State design pattern:

The state pattern is one of the behavioral design patterns. The class behavior is changed based on its state.

The State design pattern is used when an object changes its behavior based on its internal state.

Facts about state design pattern

  • If the objects have multiple states, and objects behave differently based on states
  • It encapsulates different behavior for the same object, based on its internal state
  • It helps us to keep the code cleaner without many if/else statements

State design pattern in JavaScript:

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

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

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

export class Context {
    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);
    }
}

// The client or the caller: (client.ts)
import { Context, startOrder, finishOrder } from './state'


let context: Context = new Context(new startOrder());
context.request();

context = new Context(new finishOrder());
context.request();

State design pattern in Java:

    public interface State {
        public void processOrder(Context context);
    }

    public class StartOrder implements State {

        public void processOrder(Context context) {
            System.out.println("The order processing has started.");
            context.setState(this);
        }

        public String toString(){
            return "Start State";
        }
    }


    public class FinishedOrder implements State {

        public void processOrder(Context context) {
            System.out.println("The order processing is completed.");
            context.setState(this);
        }

        public String toString(){
            return "Finished State";
        }
    }


    public class Context {
        private State state;

        public Context(){
            state = null;
        }

        public void setState(State state){
            this.state = state;
        }

        public State getState(){
            return state;
        }
    }



    public class StatePattern {
        public static void main(String[] args) {

            Context context = new Context();
            
            var startOrder = new StartOrder();
            startOrder.processOrder(context);
            System.out.println(context.getState());

            var finishedOrder = new FinishedOrder();
            finishedOrder.processOrder(context);
            System.out.println(context.getState());
        }
    }
Reference: