Please enable JavaScript.
Coggle requires JavaScript to display documents.
FACTORY METHOD PATTERN - Coggle Diagram
FACTORY METHOD PATTERN
Simple Factory
-
In the simple factory pattern we don't use polymorphism to switch between the different logics of instantiating an object. We only have a concrete implementation we don't make a contract so we can then switch the logic
Factory Method
Definition
- The factory method pattern defines an interface / superclass (contract) for creating an object but let's subclasses decide which class to instantiate and what parameters to pass to a class. Factory method let's the class defer instantiation to subclasses
The key in the factory pattern is that in the end we want an object
- We don't know how we want to construct that object
- Why we want to construct that object
- What parameters we want to pass when constructing that object
We want somebody else (factory) take these decisions.
Let's the class defer instantiation to subclasses
- The AnimalFactory should not make the decision of which AnimalClass to instantiate and what parameter to pass
- The subclasses of AnimalFactory should decide what object should construct and what parameters to pass
GENERALIZED UML
Product
-
- The data we want to produce
-
-
DEPENDENCY INJECTION
PROVIDING CODE WITH A FACTORY
- We are able to inject a particular factory in our code so we have a method to create a product
- Instead of using the new to instantiate that product we use the factory to get the product
ABSTRACT FACTORY
- We have a factory with multiple factory methods
Abstract Factory
Definition
The abstract factory pattern provides an interface for creating family of related objects without specifying their concrete class
-
UML
-
-
Difference
Any factory does not have only the possibility to create a single product but has also the capability to produce multiple products
-
-
-
USE CASES
App
-
-
- When we are creating the ui we use a single factory
- LightThemeFactory or DarkThemeFactory
- And when we need a particular control we ask the factory for that particular control
-