1. Application Scenarios
(1) Can save the state of the object, and can revert to the previous state
2. Code implementation
#!/usr/bin/env python#! _*_ coding:utf-8 _*_classoriginator (object):" "This is the basic class" " def __init__(self, State): Self.state= StatedefCreatememento (self):" "Create a Memo" " returnMemento (self.state)defSetmemento (Self, memento):" "Recover from Memo" "self.state=memento.stateclassMemento (object):" "then the memo class" " def __init__(self, State): Self.state= Stateclasscaretaker (object):" "then the memo management class" " def __init__(self):Pass defSetmemento (Self, memento):" "use a memo as a reference" "Self.memento=MementodefGetmemento (self):" "getting references from management" " returnSelf.mementoif __name__=="__main__": Originator= Originator ("state1") PrintOriginator.state Memento=Originator.creatememento () caretaker=caretaker () Caretaker.setmemento (memento) originator.state="State2" Printoriginator.state Originator.setmemento (Caretaker.getmemento ())PrintOriginator.state
Results:
/users/liudaoqiang/pycharmprojects/numpy/venv/bin/python/users/liudaoqiang/project/python_project/day22_ memento/Memento_test.pystate1state2state1process finished with exit code 0
Python design mode 22nd Day "Memo mode"