State API Documentation

class Context[source]

Bases: object

The Context defines the interface of interest to clients and maintains a reference to an instance of a State subclass representing the current state.

__init__()[source]

Initialize the context with an initial state.

set_state(state)[source]

Change the current state of the context.

Parameters:

state (State) – The new state to transition to.

request()[source]

Delegate the request to the current state.

class State[source]

Bases: ABC

The State interface declares methods that all Concrete States should implement.

abstractmethod handle(context)[source]

Handle the request and transition the context to another state if needed.

Parameters:

context (Context) – The context in which the state operates.

class ConcreteStateA[source]

Bases: State

ConcreteStateA implements the behavior associated with a state.

handle(context)[source]

Handle the request, log the current state and transition to ConcreteStateB.

Parameters:

context (Context) – The context in which the state operates, allowing state transitions.

class ConcreteStateB[source]

Bases: State

ConcreteStateB implements the behavior associated with a state.

handle(context)[source]

Handle the request, log the current state and transition to ConcreteStateA.

Parameters:

context (Context) – The context in which the state operates, allowing state transitions.