2928. Design Pattern - State
State Pattern


Behavioral Pattern: State Pattern.

1. State Pattern

The State pattern allows an object to change its behavior when its internal state changes.

2. Example

2.1 State

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

public class StartState implements State {

    public void doAction(Context context) {
        System.out.println("Player is in start state");
        context.setState(this);
    }

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

public class StopState implements State {

    public void doAction(Context context) {
        System.out.println("Player is in stop state");
        context.setState(this);
    }

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

2.2 Context

public class Context {
    private State state;

    public Context(){
        state = null;
    }

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

    public State getState(){
        return state;
    }
}

Output

Player is in start state
Start State
Player is in stop state
Stop State

3. Source Files

4. References