程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design pattern - behavioral pattern - memo pattern

編輯:Python

Catalog

List of articles

  • Catalog
  • Memo mode
  • Application scenarios
  • Code example

Memo mode

Memo mode , Without breaking the closure , Capture the internal state of an object , And save the state outside the object . In this way, the object can be restored to the original saved state later .

Simply speaking , That is, we can record a certain state during the operation , Restore the current state when encountering errors , This is designed to handle exceptions in business processes .

advantage

  • Sometimes the internal information of some initiator objects must be stored outside the initiator object , But it must be read by the initiator object itself . At this time , Using the memo mode can shield the complex internal information of the initiator from other objects , Thus, the boundary of the package can be properly maintained .
  • This model simplifies the originator . Sponsors no longer need to manage and save versions of their internal state , Clients can manage the versions of these states they need .
  • When the state of the initiator role changes , It is possible that this state is invalid , At this time, the temporarily stored memo can be used to restore the State .

shortcoming

  • If the status of the initiator role needs to be completely stored in the memo object , Then the memo object will be very expensive in terms of resource consumption .
  • When the lead role stores a memo , The person in charge may not know how much storage space this state will occupy , Thus, the user cannot be reminded whether an operation is expensive .
  • When the state of the initiator role changes , It is possible that this agreement is invalid . If the success rate of state change is not high , Better take “ If ” Protocol mode .

Application scenarios

Code example

Entity role :

  1. Originator( Originator ): Responsible for creating a Memento( Memorandum ), It is used to record the internal state of the current moment , And you can use the memo to restore the internal state .Originator It's up to you to decide Memento What internal states are stored .

  2. Memento( Memorandum ): Responsible for the storage Originator The internal state of an object , And can prevent Originator Access memo for other objects . The memo has two interfaces :

    1. Caretaker You can only see the narrow interface of the memo , He can only pass the memo to other people .
    2. Originator But you can see the broad interface of the memo , Allow it to access all the data needed to return to the previous state .
  3. Caretaker( managers ): be responsible for Memento, Not right Memento Access or operate on the content of .

class AddNumber:
def __init__(self):
self.start = 1
def add(self, number):
self.start += number
print(self.start)
class Memento:
""" Memorandum """
def backups(self, obj=None):
""" Set backup method :param obj: :return: """
self.obj_dict = copy.deepcopy(obj.__dict__)
print(" The backup data :{}".format(self.obj_dict))
def recovery(self, obj):
""" Restore backup method :param obj: :return: """
obj.__dict__.clear()
obj.__dict__.update(self.obj_dict)
return obj
if __name__ == '__main__':
test = AddNumber()
memento = Memento()
for i in [1, 2, 3, 'n', 4]:
if i == 2:
memento.backups(test)
try:
test.add(i)
except TypeError as e:
print(e)
print(test.start)
memento.recovery(test)
print(test.start)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved