#理解
#Memento模式的关键是不破坏封装
#Originator
class Originator:
def __init__(self,sta):
self.state=sta
def SetMemento(self,Mem):
self.state=Mem.getState()
def CreateMento(self):
return Memento(self.state)
class Memento:
def __init__(self,state):
self.SetState(state)
def getState(self):
return self.state
def SetState(self,state):
self.state=state
if __name__=="__main__":
ori=Originator("hello")
print ori.state
mem=ori.CreateMento()
ori.state="world"
print ori.state
ori.SetMemento(mem)
print ori.state
23种设计模式之python实现--Memento模式
最新推荐文章于 2021-07-10 18:11:55 发布