Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think its clear that nobody really understands your question. I would suggest putting it in context and making it shorter. As an example, here's one possible implementation of the state pattern in python, please study it to get an idea.</p> <pre><code>class State(object): def __init__(self, name): self.name = name def __repr__(self): return self.name class Automaton(object): def __init__(self, instance, start): self._state = start self.transitions = instance.transitions() def get_state(self): return self._state def set_state(self, target): transition = self.transitions.get((self.state, target)) if transition: action, condition = transition if condition: if condition(): if action: action() self._state = target else: self._state = target else: self._state = target state = property(get_state, set_state) class Door(object): open = State('open') closed = State('closed') def __init__(self, blocked=False): self.blocked = blocked def close(self): print 'closing door' def do_open(self): print 'opening door' def not_blocked(self): return not self.blocked def transitions(self): return { (self.open, self.closed):(self.close, self.not_blocked), (self.closed, self.open):(self.do_open, self.not_blocked), } if __name__ == '__main__': door = Door() automaton = Automaton(door, door.open) print 'door is', automaton.state automaton.state = door.closed print 'door is', automaton.state automaton.state = door.open print 'door is', automaton.state door.blocked = True automaton.state = door.closed print 'door is', automaton.state </code></pre> <p>the output of this programm would be:</p> <pre><code>door is open closing door door is closed opening door door is open door is open </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload