标签:
# coding=utf-8
# *状态模式:一个方法的判断逻辑太长,就不容易修改。方法过长,其本质就是,
# * 就是本类在不同条件下的状态转移。状态模式,就是将这些判断分开到各个能
# * 表示当前状态的独立类中
class State:
def persion_state(self):
pass
class SomeDay:
def __init__(self):
self.time = 8
self.current = Breakfast()
def set_state(self, temp):
self.current = temp
def persion_state(self):
self.current.persion_state(self)
class Breakfast(State):
def persion_state(self, some_day):
if (some_day.time < 8):
print "吃早饭"
else:
some_day.set_state(Lunch())
some_day.persion_state()
class Lunch(State):
def persion_state(self, some_day):
if (some_day.time < 12):
print "吃午饭"
else:
print "吃晚饭"
if __name__ == "__main__":
some_day = SomeDay()
some_day.time = 11
some_day.persion_state()
some_day.time = 15
some_day.persion_state()
标签:
原文地址:http://www.cnblogs.com/zijiyanxi/p/5281370.html