# -*- coding:UTF-8 -*- class Originator: def __init__(self): self.__state = None @property def state(self): return self.__state @state.setter def state(self, value): self.__state = value def create_memento(self): return Memento(self.__state) def set_memento(self, memento): self.__state = memento.state def show(self): print("State=%s" % self.__state) class Memento: def __init__(self, state): self.__state = state @property def state(self): return self.__state class Caretaker: def __init__(self): self.__memento = None @property def memento(self): return self.__memento @memento.setter def memento(self,value): self.__memento = value if __name__=="__main__": o = Originator() o.state = "On" o.show() c = Caretaker() c.memento = o.create_memento() o.state = "Off" o.show() o.set_memento(c.memento) o.show()
原文地址:https://www.cnblogs.com/gjinwei/p/8350770.html
时间: 2024-11-18 13:33:05