implemented prototype pattern

This commit is contained in:
tylerlaberge
2016-07-28 22:31:22 -04:00
parent fd62ae8ea9
commit 30ecd62917
2 changed files with 129 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
from copy import deepcopy
from types import MethodType
class Prototype(object):
"""
Prototype design pattern abstract class.
"""
def copy(self, **attributes):
"""
Copy this object and optionally update attributes.
@param attributes: Keyword arguments of any attributes you wish to update.
@return: A copy of this object with the updated attributes.
"""
obj = deepcopy(self)
for attribute in attributes:
if callable(attributes[attribute]):
setattr(obj, attribute, MethodType(attributes[attribute], obj))
else:
setattr(obj, attribute, attributes[attribute])
return obj