YouTip LogoYouTip

Python State

The core idea of the State pattern is to encapsulate an object's state into independent classes and delegate the object's behavior to the current state object. When the object's state changes, it switches to a different state object, thereby changing its behavior. ### Why do we need the State pattern? Without using the State pattern, we typically use a large number of `if-else` or `switch-case` statements within a class to handle behaviors in different states. This approach has several problems: * **Bloated code**: As the number of states increases, the conditional statements become increasingly complex * **Hard to maintain**: Modifying the behavior of one state might affect other states * **Violates the Open-Closed Principle**: Adding a new state requires modifying existing code The State pattern solves these problems by encapsulating each state into an independent class. * * * ## Structure of the State Pattern ### Main Roles The State pattern contains three core roles: 1. **Context**: Maintains an instance of a concrete state object, which defines the current state 2. **State (State Interface)**: Defines an interface for encapsulating behavior related to a specific state of the Context 3. **ConcreteState**: Implements the State interface; each concrete state class implements behavior related to the Context's state ### UML Class Diagram !(#) * * * ## Implementation of the State Pattern ### Basic Implementation Steps Let's understand the implementation of the State pattern through a simple example. Suppose we have an elevator system, and the elevator has different states: door open, door closed, moving, stopped. #### Step 1: Define the State Interface ## Instance from abc import ABC, abstractmethod class ElevatorState(ABC): """Elevator state interface""" @abstractmethod def open_doors(self): """Open door operation""" pass @abstractmethod def close_doors(self): """Close door operation""" pass @abstractmethod def move(self): """Move operation""" pass @abstractmethod def stop(self): """Stop operation""" pass #### Step 2: Implement Concrete State Classes ## Instance class DoorOpenState(ElevatorState): """Door open state""" def open_doors(self): print("Door is already open") return self def close_doors(self): print("Closing door...") return DoorClosedState() def move(self): print("Error: Door is open, cannot move") return self def stop(self): print("Elevator is already stopped") return self class DoorClosedState(ElevatorState): """Door closed state""" def open_doors(self): print("Opening door...") return DoorOpenState() def close_doors(self): print("Door is already closed") return self def move(self): print("Elevator starts moving...") return MovingState() def stop(self): print("Elevator is already stopped") return self class MovingState(ElevatorState): """Moving state""" def open_doors(self): print("Error: Elevator is moving, cannot open door") return self def close_doors(self): print("Door is already closed") return self def move(self): print("Elevator is moving") return self def stop(self): print("Elevator is stopping...") return DoorClosedState() #### Step 3: Create the Context Class ## Instance class Elevator: """Elevator context class""" def __
← Python MediatorPython Iterator β†’