Commit d68190d
committed
Simplified the Abstract Factory Pattern to be Pythonic
Once we appreciate that:
1) classes are their own factories and
2) classes are first class objects in Python,
this pattern is massively simpler in idiomatic Python. Additional factory
classes are not needed.
This change also removes the `get_food` method because:
1) This violates the single responsibility principle of the factory
2) It obscures the main point.
If we want an association between animals and what food they eat we should either:
1) make it part of the Pet interface
2) or, have another callable which can make this decision
(presumably it should be passed the animal instance in order to do so).
Or, if we really need it, create an interface that does both creation and
feeding as before, but to have that in the this pattern obscures the
simplicity of how this can and should be implemented in Python.
The second implementation was also deleted. This simply looks up the
classes using a string name, which is pointless when you can just
use the class itself (why use `"kitty"` when you can use `Cat`).1 parent ac79516 commit d68190d
1 file changed
+25
-71
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
12 | 18 | | |
13 | 19 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
18 | 24 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
55 | 54 | | |
56 | 55 | | |
57 | | - | |
58 | | - | |
59 | 56 | | |
60 | 57 | | |
61 | 58 | | |
| |||
74 | 71 | | |
75 | 72 | | |
76 | 73 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
| 74 | + | |
96 | 75 | | |
97 | | - | |
98 | | - | |
| 76 | + | |
| 77 | + | |
99 | 78 | | |
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 | | - | |
| 79 | + | |
126 | 80 | | |
127 | 81 | | |
128 | 82 | | |
129 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
130 | 92 | | |
131 | | - | |
132 | 93 | | |
133 | 94 | | |
134 | 95 | | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | 96 | | |
139 | 97 | | |
140 | 98 | | |
141 | 99 | | |
142 | | - | |
| 100 | + | |
143 | 101 | | |
144 | 102 | | |
145 | 103 | | |
146 | | - | |
147 | 104 | | |
148 | 105 | | |
149 | 106 | | |
150 | | - | |
151 | 107 | | |
152 | | - | |
153 | | - | |
0 commit comments