-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpts-4.py
162 lines (108 loc) · 3.71 KB
/
pts-4.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Python Thoughts Snippet #4 - Factory Method - Creational Pattern
# Python 3.7
# 2019/08/31
# post: https://pythonicthoughtssnippets.github.io/#4-factory-method-creational-pattern
# THIS CODE IS NOT MEANT TO BE FUNCTIONAL OR EXECUTABLE,
# IT IS A REPRESENTATION OF AN IDEA AND AN EXAMPLE TO RAISE DISCUSSION.
# As a common practice in my Pythonic Thoughts Snippets,
# I am not going into the details of the minor implementations,
# on doubts please search elsewhere on the web, there are countless of
# amazing explanations; here, we focus on the broader concept
# EDIT 2019/09/05
# There's a very interesting fault in the design shown in the original example
# of this post, I came to understand it when I was actually using the pattern.
# Can you spot it before hand? Bellow is the original example from this post,
# and, at the end, an updated explanation.
# The original example:
from abc import ABC, abstractmethod
class Interface(ABC):
def __new__(cls, arg1):
match_d = {
# evalutes arguments based on conditions to decide which
# subclass to instantiate
is_for_subclass1(arg1): SubClass1,
is_for_subclass2(arg1): SubClass2,
# etc...
}
try:
# this actually calls object.__new__(match_d[True])
# dont know if it is better to super(Interface) or object.
return super(Interface, cls).__new__(match_d[True])
except KeyError:
raise NotImplementedError from None
@abstractmethod
def run(self):
return
class SubClass1(Interface):
def __init__(self, arg1):
# do init magic
def run(self):
#run magic
self._private_meth_1()
self._private_meth_2()
return
def _private_meth_1(self):
return
def _private_meth_2(self):
return
class SubClass2(Interface):
def __init__(self, arg1):
# do init magic
def run(self):
#run magic
self._private_meth_1()
self._private_meth_2()
return
def _private_meth_1(self):
return
def _private_meth_2(self):
return
def _private_meth_3(self):
return
# An updated discussion:
from abc import ABC, abstractmethod
class Interface:
def __new__(cls, arg1):
match_d = {
# evalutes arguments based on conditions to decide which
# subclass to instantiate
is_for_subclass1(arg1): SubClass1,
is_for_subclass2(arg1): SubClass2,
# etc...
}
try:
# this actually calls object.__new__(match_d[True])
# dont know if it is better to super(Interface) or object.
return super(Interface, cls).__new__(match_d[True])
except KeyError:
raise NotImplementedError from None
class ClassBase(ABC):
@abstractmethod
def run(self):
return
class SubClass1(ClassBase):
def __init__(self, arg1):
# do init magic
def run(self):
#run magic
self._private_meth_1()
self._private_meth_2()
return
def _private_meth_1(self):
return
def _private_meth_2(self):
return
class SubClass2(ClassBase):
def __init__(self, arg1):
# do init magic
def run(self):
#run magic
self._private_meth_1()
self._private_meth_2()
return
def _private_meth_1(self):
return
def _private_meth_2(self):
return
def _private_meth_3(self):
return