forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype_concept.py
57 lines (45 loc) · 1.79 KB
/
prototype_concept.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# pylint: disable=too-few-public-methods
# pylint: disable=arguments-differ
"Prototype Concept Sample Code"
from abc import ABCMeta, abstractmethod
class IProtoType(metaclass=ABCMeta):
"interface with clone method"
@staticmethod
@abstractmethod
def clone():
"""The clone, deep or shallow.
It is up to you how you want to implement
the details in your concrete class"""
class MyClass(IProtoType):
"A Concrete Class"
def __init__(self, field):
self.field = field # any value of any type
def clone(self):
" This clone method uses a shallow copy technique "
return type(self)(
self.field # a shallow copy is returned
# self.field.copy() # this is also a shallow copy, but has
# also shallow copied the first level of the field. So it
# is essentially a shallow copy but 2 levels deep. To
# recursively deep copy collections containing inner
# collections,
# eg lists of lists,
# Use https://docs.python.org/3/library/copy.html instead.
# See example below.
)
def __str__(self):
return f"{id(self)}\tfield={self.field}\ttype={type(self.field)}"
# The Client
OBJECT1 = MyClass([1, 2, 3, 4]) # Create the object containing a list
print(f"OBJECT1 {OBJECT1}")
OBJECT2 = OBJECT1.clone() # Clone
# Change the value of one of the list elements in OBJECT2,
# to see if it also modifies the list element in OBJECT1.
# If it changed OBJECT1s copy also, then the clone was done
# using a 1 level shallow copy process.
# Modify the clone method above to try a 2 level shallow copy instead
# and compare the output
OBJECT2.field[1] = 101
# Comparing OBJECT1 and OBJECT2
print(f"OBJECT2 {OBJECT2}")
print(f"OBJECT1 {OBJECT1}")