Week 5 - Factory Design Pattern
The Factory Design Pattern¶
Classification¶
- Creational Design Pattern
Pattern Definition¶
Factory Method¶
- Define an interface for creating an object, but let sub-classes decide which class to instantiate. Factory Method lets a class defer instantiation to sub-classes.
Abstract Factory¶
- Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Representations¶
Mermaid Graph¶
Factory Method¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Abstract Factory¶
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 |
|
UML¶
Factory Method¶
Abstract Factory¶
Usage¶
Factory Method¶
- The factory method design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
- Super class in factory design pattern can be an interface, abstract class or a normal java class
Abstract Factory¶
- Similar to the Factory Method pattern, BUT the Abstract Factory pattern, gets rid of the factory method’s switch/if-else block and uses a class for each product sub-class. Then an Abstract Factory class that will return the sub-class based on the input factory class.
Real World Usages¶
- Nearly all frameworks that allow you to do things like swap out the database, cache, or other backend being used for data storage and retrieval. Spring, laravel, etc.
Application to my Real World Application¶
- Use a factory method pattern to build different processors for my pipelines depending on user input. Maybe I want a stateless pipeline, or maybe I want a stateful pipeline. Maybe I’d like a pipeline that gets interrupted if an error occurs (Interruptable pipeline).
Java Code Example¶
Main Take-Aways from the textbook¶
- All factory patterns promote loose coupling by reducing the dependency of your application on concrete classes.
- The intent of Factory Method is to allow a class to defer instantiation to its subclasses.
- The intent of Abstract Factory is to create families of related objects without having to depend on their concrete classes.
- Factories are a powerful technique for coding to abstractions, not concrete classes.
- The Dependency Inversion Principle guides us to avoid dependencies on concrete types and to strive for abstractions.
Main OO Principles of Factory Pattern¶
Factory Method¶
- Encapsulates Object Creation
- Removes the instantiation of actual implementation classes from client code.
- Provides an approach to code for interface rather than implementation.
- Makes our code more robust with loose coupling. Thus easier to extend classes.
- Provides abstraction between implementation and client classes through inheritance.
Abstract Factory¶
- Is robust and avoids conditional logic of Factory Method pattern via composition (object creation is implemented in exposed methods of factory interface).
- Encapsulates Object Creation
- Decouples clients from concrete classes
- Provides an approach to code for interface rather than implementation.
- Makes our code more robust with loose coupling
- AKA the factory of factories and can be easily extended to accommodate more products.