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

备忘录模式实例



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
  • android studio模拟器出现未知错误2024-11-22 13:01:04
  • 01背包问题动态规划详解2024-11-22 13:01:04
  • linux ifconfig只有lo2024-11-22 13:01:04
  • java单元测试类2024-11-22 13:01:04
  • 主板自检时间长怎么办2024-11-22 13:01:04
  • 倒排索引原理和实现2024-11-22 13:01:04
  • css烟花特效2024-11-22 13:01:04
  • 黑客工具app2024-11-22 13:01:04