class People(object): class Workday(State): default = True @behavior def day(self): print 'work hard.' class Weekend(State): @behavior def day(self): print 'play harder!' people = People() while True: for i in xrange(1, 8): if i == 6: switch(people, People.Weekend) if i == 1: switch(people, People.Workday) people.day()
def _shootINPUT(self, aState, aButton, aValue): ''' Guns are active and triggers are pressed. Watch for release of either trigger and end shooting if so. ''' handled = False #If button is release check if it's start or select # if so, exit shooting #Only check release events, we should never get a pressed event # for either trigger as while in this state they are pressed if not (aValue & 0x01): if (aButton == ecodes.BTN_TL2) or (aButton == ecodes.BTN_TR2): if self._combatstate != self._armedstate: self._combatstate = state.switch(self._combatstate, self._armedstate) handled = True return handled
def _armedINPUT(self, aState, aButton, aValue): ''' Check for fire start or combat mode state switch. ''' handled = self._checkCombatSwitch(aButton, aValue) if not handled: other = None if aValue & 0x01: if aButton == ecodes.BTN_TL2: other = ecodes.BTN_TR2 handled = True elif aButton == ecodes.BTN_TR2: other = ecodes.BTN_TL2 handled = True if handled: sv, _ = self._getPressed(other) if sv & 0x01: #Switch to the shoot start state self._combatstate = state.switch( self._combatstate, self._shootstartstate) return handled
people.day = weekend if i == 1: people.day = workday people.day() #################################### @stateful class People(object): class Workday(State): default = True @behavior def day(self): print('work hard.') class Weekend(State): @behavior def day(self): print('play harder!') people = People() while True: for i in xrange(1, 8): if i == 6: switch(people, People.Weekend) if i == 1: switch(people, People.Workday) people.day()
#coding:utf8 ''' 状态模式测试 ''' import time from state import curr, switch, stateful, State, behavior @stateful class People(object): class Workday(State): default = True @behavior def day(self): print 'work hard.' class Weekend(State): @behavior def day(self): print 'play harder!' people = People() while 1: for i in xrange(1,8): if i == 6: switch(people, People.Weekend) if i == 1: switch(people, People.Workday) people.day() time.sleep(1)
def signIn(self, usrName, usrPwd): if usrName == 'admin' and usrPwd == 'admin': print('Successfully logged in.') #如果成功登录就切换至工作状态 state.switch(self, User.SignedIn)
def _changeCombat(self): ''' Switch combat state between _armed and _disarmed functions ''' other = self._armedstate if self._combatstate == self._disarmedstate else self._disarmedstate self._combatstate = state.switch(self._combatstate, other)
def rstate(self, aValue): self._rstate = state.switch(self._rstate, aValue)
def lstate(self, aValue): self._lstate = state.switch(self._lstate, aValue)