Lesson 1 - The Factory Pattern
In this series we will be going through the various design patterns that are available. I will be using many different sources to gather information and all these sources will be listed in the links below. I will also be heavily referencing from the Head First Design Patterns Book which is definitely worth a read. At the bottom of each article there will also be links to some code examples for you to go through and play around with. So lets begin.
The Factory Pattern is a Creational Design Pattern.
Creational Design Patterns are design patterns that deal with object creation mechanisms.
In this tutorial we will look at the following :
- Simple Factory
- Factory Method.
- Abstract Factory.
Definition
Factory Method
“The Factory Method Pattern defines an interface for creating an object but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to sub classes.” - Head First Design Patterns (Page 134)
Abstract Factory
“The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.” - Head First Design Patterns (Page 156)
When to use ?
When the object creation process is complex or multiple objects created that share the same properties. When business logic is required to determine what object is created.
Important to Note
All factories encapsulate object creation.
The new keyword is not needed as instantiation is done in the factory implementation.
When you think new, think concrete. (Thats an implementation not an interface).
All factory patterns promote loose coupling by reducing the dependency of your application on concrete classes Concrete classes are more fragile and less flexible.
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification (Open-Closed Priciple)
Instead of having multiple new MyObject() call, the initialization is kept in one place, satisfying the Don’t Repeat Yourself(DRY) principle.
Simple Factory
While not actually a pattern. It is a simple way to decouple your clients from the concrete classes.
Factory Method
Provides a generic interface to create objects.
Provides an interface for creating one product.
A factory method handles object creation and encapsulates it in a subclass, This decouples the client code in the subclass from the object creation code in the superclass.
Subclasses decide what needs to be created.
The factory method adheres to the Dependency Inversion Principle. (Depend on Abstracts and not concrete classes)
Creates objects via inheritance. ( Extend a class and overwrite the factory method )
Abstract Method
Creates objects via object inheritance. (Defines an abstract type for creating a family of products. Subclasses of this type define how those products are produced. To use this factory you instantiate one and pas sin some code that is written against the abstract type.)
Interface needs to change when new products are added.(Interfaces of subclasses will therefore also need to be updated)
Uses Factory Methods to implement its concrete classes.
Useful when you want to create a family of products.
Benefits
- Provides Decoupling
- Avoids duplication
- Single point of maintenance.
- By allowing Clients to code to a interface and not an
implementation, you allow the code to be more extensible and
flexible.
Code Examples
The examples below are taken from the Head First Design Patterns Book. Work through them in the given order.
The Pizza Problem
Simple Factory
Factory Method
Abstract Factory
Links
- https://medium.com/@coyarzun/head-first-design-patterns-factory-pattern-2e1a9cfc119d
- https://wanago.io/2019/12/02/javascript-design-patterns-factories-typescript/
- https://blog.fullstacktraining.com/factory-pattern-in-typescript/
- https://www.javatpoint.com/factory-method-design-pattern
- https://www.wikiwand.com/en/Factory_method_pattern#/See_also
- https://medium.com/@info.anikdey003/factory-method-design-pattern-277dd4bd3a11
- https://www.wikiwand.com/en/Creational_pattern
- https://www.baeldung.com/java-constructors-vs-static-factory-methods
- https://thedulinreport.com/2017/07/30/design-patters-in-typescript-factory/
- https://github.com/gztchan/design-patterns-in-typescript/blob/master/abstract-factory/abstract-factory.ts
- https://www.wikiwand.com/en/Abstract_factory_pattern
More Reading
Next Up - The Strategy Pattern
Comments
Post a Comment