状态模式,就像某一个人在儿童、青年、老年所展现的状态是不同的。
class Obj(object):
def __init__(self, status):
self.status = status
def do(self):
which_do = {"low": self.low_do,
"middle": self.middle_do,
"high": self.high_do
}
which_do[self.status]()
def low_do(self):
print("low_do")
def middle_do(self):
print("middle_do")
def high_do(self):
print("high_do")
def main():
obj = Obj('low')
obj.do()
obj.status = 'middle'
obj.do()
if __name__ == '__main__':
main()
网友评论