You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learning-python-design-patterns/notes.md
+43-1Lines changed: 43 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -146,4 +146,46 @@ Python:
146
146
- Singletons have a global point of access
147
147
- Al classes that are dependent on global variables get tightly coupled as a change to the global data by one class can inadvertently impact the other class
148
148
149
-
# Ch3. The factory pattern - building factories to create objects
149
+
# Ch3. The factory pattern - building factories to create objects
150
+
151
+
## Understanding the factory pattern
152
+
- Factory = a class that is responsible for creating objects of other types
153
+
- The class that acts as a factory has an object and methods associated with it
154
+
- The client calls this method with certain parameters; objects of desired types are created in turn and returned to the client by the factory
155
+
156
+
**Advantages**
157
+
- Loose coupling: object creation can be independent of the class implementation
158
+
- The client only needs to know the interface, methods, and parameters that need to be passed to create objects of the desired type (simplifies implementations for the client)
159
+
- Adding another class to the factory to create objects of another type can be easily done without the client changing the code
160
+
161
+
## The simple factory pattern
162
+
- Not a pattern in itself
163
+
- Helps create objects of different types rather than direct object instantiation
164
+
165
+
## The factory method pattern
166
+
- We define an interface to create objects, but instead of the factory being responsible for the object creation, the responsibility is deferred to the subclass that decides the class to be instantiated
167
+
- Creation is through inheritance and not through instantiation
168
+
- Makes the design more customizable. It can return the same instance or subclass rather than an object of a certain type
169
+
170
+
> The factory method pattern defines an interface to create an object, but defers the decision ON which class to instantiate to its subclasses
171
+
172
+
**Advantages**
173
+
- Makes the code generic and flexible, not being tied to a certain class for instantiation. We're dependent on the interface (Product) and not on the ConcreteProduct class
174
+
- Loose coupling: the code that creates the object is separate from the code that uses it
175
+
- The client don't need to bother about what argument to pass and which class to instantiate -> the addition of new classes is easy and involves low maintenance
176
+
177
+
## The abstract factory pattern
178
+
179
+
> Provide an interface to create families of related objects without specifying the concrete class
180
+
181
+
- Makes sure that the client is isolated from the creation of objects but allowed to use the objects created
0 commit comments