Status mode (Python)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.