@@ -56,6 +56,7 @@ Here **polymorphism** is often used.
5656
5757### Factory method
5858Factory method defines an interface for creating an object but defers object instantiation to run time.
59+
5960``` python
6061from abc import ABC , abstractmethod
6162
@@ -118,12 +119,14 @@ square: Shape = factory.shape()
118119print (square.__class__ .__name__ )
119120print (square.draw())
120121```
122+
121123Factory encapsulates objects creation. Factory is an object that is specialized in creation of other objects.
122124- Benefits:
123125 - Useful when you are not sure what kind of object you will be needed eventually.
124126 - Application need to decide what class it has to use.
125127- Exercise:
126128 - Pet shop is selling dogs but now it sells cats too.
129+
127130``` python
128131from abc import ABC , abstractmethod
129132
@@ -325,6 +328,7 @@ Python has global variables and modules which are **_singletons_**. Singleton al
325328Useful if you want to share cached information to multiple objects.
326329
327330** Classic singleton**
331+
328332``` python
329333from typing import Any, Dict
330334
@@ -372,6 +376,7 @@ print(bar_one is bar_two)
372376```
373377
374378** Borg singleton**
379+
375380``` python
376381from typing import Dict, Any
377382
@@ -417,6 +422,7 @@ Builder reduces complexity of building objects.
417422 - Product: object being built
418423- Exercise:
419424 - Build a car object
425+
420426``` python
421427from abc import ABC , abstractmethod
422428
@@ -1427,6 +1433,7 @@ Composite pattern is related to iterator pattern.
14271433 - Will iterate over a certain point based on client input
14281434
14291435** Iterator function**
1436+
14301437``` python
14311438from typing import Iterator, Tuple, List
14321439
@@ -1529,6 +1536,7 @@ This type of pattern decouples responsibility. Composite is related to this desi
15291536 - Successor
15301537 - Concrete Handler
15311538 - Checks if it can handle the request
1539+
15321540``` python
15331541from abc import abstractmethod
15341542from typing import List
0 commit comments