Python design pattern-body mode

Source: Internet
Author: User

Demand, return to work status based on current time

#Encoding=utf-8__author__='[email protected]'defget_state (hour):ifHour>=8 andHour<=12:        return 'morning work, Spirit hundredfold'    elifHour>12 andHour<=14:        return 'work at noon, sleepy, want to have lunch break'    elifHour>14 andHour<=18:        return 'afternoon work, good condition'    elifHour>18 andHour<=20:        return 'working overtime, not very good condition.'    Else:        return 'No , I'm going to bed.'if __name__=='__main__':     forHourinch(9,11,12,13,14,17,19,22,23):        Print '%d points,'%hour,get_state (Hour)

The disadvantage is that if the state is very long, (now 5 kinds), this code will be very lengthy, so later maintenance is more difficult

State mode

#Encoding=utf-8__author__='[email protected]' fromAbcImportAbcmeta, AbstractmethodclassState ():__metaclass__=Abcmeta @abstractmethoddefWrite_code (self):PassclassMorning (state):defWrite_code (self, work):ifWork.hour <= 12 andWork.hour > 8:            Print 'morning work, Spirit hundredfold'        Else: Work.set_status (Noon ()) Work.write_code (work)classNoon (state):defWrite_code (self, work):ifWork.hour <= 14 andWork.hour>12 :            Print 'work at noon, sleepy, want to have lunch break'        Else: Work.set_status (Afternoon ()) Work.write_code (work)classafternoon (State):defWrite_code (self, work):ifWork.hour <= 18 andWork.hour>14:            Print 'afternoon work, good condition'        Else: Work.set_status (Eve ()) Work.write_code (work)classEve (state):defWrite_code (self, work):ifWork.hour <= 22 andWork.hour>18:            Print 'working overtime, not very good condition.'        Else: Work.set_status (Night ()) Work.write_code (work)classNight (state):defWrite_code (self, work):ifWork.hour <= 8orWork.hour > 22:            Print 'No , I'm going to bed.'        Else: Work.set_status (Morning ()) Work.write_code (work)classWork ():def __init__(self, Hour): Self.hour=hour self.state=morning ()defSet_status (Self, State): Self.state= StatedefWrite_code (self, Work): Self.state.write_code (work)if __name__=='__main__': Work= Work (10)     forHourinch(9, 11, 12, 13, 14, 17, 19, 22, 23,12): Work.hour=HourPrint '%d points,'%hour Work.write_code (work)

State mode, define a state of the abstract class, for each state to build a class, the class has a handle function (here is Write_code), in accordance with the work of the time to do the corresponding processing, the benefits of this is very flexible, each state has its own judgment logic. It is more convenient to expand and maintain.

Python design pattern-body mode

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.