I have a class hierarchy with a base State
class, several inheriting classes each named after their relevant state, and an FSM
class that contains all the states in a particular state machine as well as the logic for moving from one state to another. A simplified version in Python:
class State:
def __init__(self):
pass
class InitialState(State):
def __init__(self):
super().__init__()
self.name = "Initial State"
class State1(State):
def __init__(self):
super().__init__()
self.name = "State One"
class State2(State):
def __init__(self):
super().__init__()
self.name = "State Two"
class FinalState(State):
def __init__(self):
super().__init__()
self.name = "Final State"
class FSM:
def __init__(self):
self.states = {
'initial': InitialState(),
'state1': State1(),
'state2': State2(),
'final': FinalState()
}
self.current_state = self.states['initial']
def init_to_state1(self):
assert self.current_state == self.states['initial']
self.current_state = self.states['state1']
Notice in my FSM class, I'm instantiating each state and in the init_to_state1
method, I'm moving from an instantiated class to another.
Is this the right thing to do? Or is it better to instantiate each state as the implementing class is moved to it? Should I be destroying states once they've been moved out of? Is this the kinda thing where "it depends" is the right answer? I'm just trying to make sure I'm approaching this design the right way.
Thanks in advance.