shlogg · Early preview
Prashant Mishra @prashantrmishra

Behavioral Design Pattern: State Example With Java Implementation

The State design pattern changes a class's behavior based on its state. It uses an abstract state and concrete states to alter the context's behavior. Example in Java shows how it works with StartState and EndState classes.

The State is one of the Behavioral design patterns, In this the behavior of a class changes based on its state.
Key concepts:
Context: Class/object whose behavior changes based on state
State: abstract state
Concrete State: representing various states, which changes the behavior of the Context class.
Let's understand this with an example:
State.java

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

    
    

    
    




Concrete Implementation of State

public class StartState implements State {
    private Context context;
    public StartState(){}
    @Override
    p...