Implemented Memento design pattern.

This commit is contained in:
tylerlaberge
2016-08-20 15:32:07 -04:00
parent e7f7674030
commit 941408a37f
2 changed files with 105 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
from copy import deepcopy
class Memento(object):
"""
Memento class as part of the Memento design pattern.
"""
def __init__(self, state):
"""
Initialize a new Memento instance.
@param state: The state to save in this Memento.
@type state: dict
"""
self.__state = state
@property
def state(self):
return self.__state
class Originator(object):
"""
Originator base class as part of the Memento design pattern.
"""
def commit(self):
"""
Commit this objects state to a memento.
@return: A memento instance with this objects state.
"""
return Memento(deepcopy(self.__dict__))
def rollback(self, memento):
"""
Rollback this objects state to a previous state.
@param memento: The memento object holding the state to rollback to.
@type memento: Memento
"""
self.__dict__ = memento.state