The State pattern allows an object to change its behavior when its internal state changes. It is particularly useful for implementing state machines or managing state-dependent behavior.
// State interface
class State {
handle(context) {
throw new Error("This method must be overridden!");
}
}
// Concrete States
class ConcreteStateA extends State {
handle(context) {
console.log("State A handling request.");
context.setState(new ConcreteStateB());
}
}
class ConcreteStateB extends State {
handle(context) {
console.log("State B handling request.");
context.setState(new ConcreteStateA());
}
}
// Context
class Context {
constructor(state) {
this.setState(state);
}
setState(state) {
console.log(`State changed to ${state.constructor.name}`);
this.state = state;
}
request() {
this.state.handle(this);
}
}-
State Interface
- The
Stateclass defines an interface for encapsulating the behavior associated with a particular state of theContext.
- The
-
Concrete States
ConcreteStateAandConcreteStateBare concrete implementations of theStateinterface.- Each concrete state implements the
handlemethod, which performs actions specific to that state and
-
Context
- The
Contextclass maintains an instance of aStatesubclass that defines the current state. - The
setStatemethod changes the current state and logs the state transition. - The
requestmethod delegates the request to the current state'shandlemethod.
- The
const context = new Context(new ConcreteStateA());
context.request(); // State A handling request. State changed to ConcreteStateB
context.request(); // State B handling request. State changed to ConcreteStateA- A
Contextobject is created with an initial state (ConcreteStateA). - Calling
requeson the context triggers the current state'shandle method, which performs an action and transitions to the next state.
This code defines a State interface and two concrete states (ConcreteStateA and ConcreteStateB). The Context class maintains the current state and delegates state-specific behavior to the current state object. The state transitions are managed within the handle methods of the concrete states, allowing the context to change its behavior dynamically.