Complete the code to declare the interface for different states.
public interface State {
void [1]();
}The handle method is commonly used in the State pattern to define the action for each state.
Complete the code to set the current state in the context class.
public class Context { private State state; public void setState([1] state) { this.state = state; } }
The setState method accepts a parameter of type State to change the current state.
Fix the error in the state transition method to call the correct method on the current state.
public class Context { private State state; public void request() { state.[1](); } }
The request method should delegate the call to the handle method of the current state.
Fill both blanks to implement a concrete state class with the correct method and context reference.
public class [1]State implements State { private Context [2]; public [1]State(Context context) { this.[2] = context; } @Override public void handle() { // state-specific behavior } }
The class name should be ConcreteState and the context reference variable is commonly named context for clarity.
Fill all three blanks to complete the context's constructor and initial state setup.
public class Context { private State [1]; public Context() { this.[1] = new [2]State(this); } public void setState(State state) { this.[1] = state; } public void request() { [3].handle(); } }
The context holds a state variable, initializes it with a ConcreteState instance, and calls state.handle() in the request method.
