Design Patterns Java - State Php

1) What have we done?

Firstly we created interface "State"


public interface State
{
    void methodA(Context context);

    void methodB(Context context);
}




We just implemented concrete "States" = "StateOne" and "StateTwo" for possible "Context" states


public class StateOne implements State
{
    @Override
    public void methodA(Context context)
    {
        System.out.println("MethodA doing something related with state one");
        context.setStateTwo();
    }

    @Override
    public void methodB(Context context)
    {
        System.out.println("MethodB doing something related with state one");
        context.setStateTwo();
    }
}

public class StateTwo implements State
{
    @Override
    public void methodA(Context context)
    {
        System.out.println("MethodA doing something related with state two");
        context.setStateOne();
    }

    @Override
    public void methodB(Context context)
    {
        System.out.println("MethodB doing something related with state two");
        context.setStateOne();
    }
}




Now "Context" object executes proper methods from aggregated "StateOne" or "StateTwo"


public class Context
{
    protected State stateOne;

    protected State stateTwo;

    protected State currentState;

    public void methodA()
    {
        currentState.methodA(this);
    }

    public void methodB()
    {
        currentState.methodB(this);
    }

    public void setStateOne()
    {
        if (stateOne == null)
            stateOne = new StateOne();
        currentState = stateOne;
    }

    public void setStateTwo()
    {
        if (stateTwo == null)
            stateTwo = new StateTwo();
        currentState = stateTwo;
    }
}




2) What has changed in Client ?

We set initial state and call executes methods in Client


public class Client
{
    public static void main(String[] args)
    {
        Context context = new Context();
        context.setStateOne();

        context.methodA();
        context.methodB();
        context.methodB();
        context.methodA();
    }
}




This is whole conception of this pattern