当前位置:网站首页 > 技术博客 > 正文

备忘录模式实例



Python 方式:

# Memento Pattern with Python Code from abc import abstractmethod,ABCMeta # 创建Memento类 class Memento(): _state = "" def __init__(self,strState): self._state = strState def getState(self): return self._state # 创建Originator类 class Originator(): _state = "" def setState(self,strState): self._state = strState def getState(self): return self._state def saveStateToMemento(self): return Memento(self._state) def getStateFromMemento(self,inMemento): self._state = inMemento.getState() # 创建CareTaker类 class CareTaker(): _mementoList = [] def add(self,inMemento): self._mementoList.append(inMemento) def get(self,inIndex): return self._mementoList[inIndex] # 调用输出 if __name__ == '__main__': originator = Originator() careTaker = CareTaker() originator.setState("State #1") originator.setState("State #2") careTaker.add(originator.saveStateToMemento()) originator.setState("State #3") careTaker.add(originator.saveStateToMemento()) originator.setState("State #4") print("Current State: " + originator.getState()) originator.getStateFromMemento(careTaker.get(0)) print("First saved State: " + originator.getState()) originator.getStateFromMemento(careTaker.get(1)) print("First saved State: " + originator.getState())

版权声明


相关文章:

  • pop3属于哪一层协议2024-11-22 13:01:04
  • 不会c语言能学数据结构吗2024-11-22 13:01:04
  • orm框架有哪些优缺点2024-11-22 13:01:04
  • html中img标签src的作用2024-11-22 13:01:04
  • linux监测网络流量命令2024-11-22 13:01:04
  • java单元测试类2024-11-22 13:01:04
  • 黑客工具app2024-11-22 13:01:04
  • 函数void已有主体2024-11-22 13:01:04
  • 虚拟机好用的2024-11-22 13:01:04
  • 深度优先遍历需要借助什么数据结构2024-11-22 13:01:04