Status Mode:When the internal state of an object changes, it is allowed to change its behavior. This object seems to have changed its class.
Application scenariosThe behavior of an object depends on its state, that is, it must change its behavior according to its state at runtime. If the conditional expression that controls state conversion is too complex, you can consider using the state mode.
Key Points: Transfers the status judgment logic to a series of classes that indicate different states, which simplifies complicated logic judgments.
Advantages: Localized behavior related to a specific State and separated behavior in different States.
# Encoding = UTF-8 ## by Panda # status mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # state: base class state (): def writeprogram (): pass # class forenoonstate (state): def writeprogram (self, w): If (W. hour <12): printinfo ("Current Time: % d: Working Status: working in the morning, Times" % W. hour) else: W. setstate (noonstate () W. writeprogram () # class noonstate (state): def writeprogram (self, W): If (W. hour <13): printin Fo ("Current Time: % d lunch; lunch break" % W. hour) else: W. setstate (afternoonstate () W. writeprogram (); # class afternoonstate (state): def writeprogram (self, W): If (W. hour <18): printinfo ("Current Time: % d:pm status is good, continue to work hard" % W. hour) else: W. setstate (eveningstate () W. writeprogram (); # class eveningstate (state): def writeprogram (self, W): If (W. taskfinished): W. setstate (reststate () W. writeprogram () return if (W. hour <21): printinfo ("Current Time: % d: overtime, so tired! "% W. hour) else: W. setstate (sleepingstate () W. writeprogram (); # sleep state class sleepingstate (state): def writeprogram (self, W): printinfo ("Current Time: % d sleeping" % W. hour) # class reststate (state): def writeprogram (self, W): printinfo ("Current Time: % d: coming home from work" % W. hour) # context: class work (): State = forenoonstate (); taskfinished = false hour = 8.0 def setstate (self, State): Self. state = State def writeprogram (Self): Self. state. writeprogram (Self) def clientui (): Work = work () For I in range (9, 23, 1): work. hour = I if (I> 19): work. taskfinished = true work. writeprogram () returnif _ name _ = '_ main _': clientui ();
Class Diagram: