Factory Method
Contents
Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created
The Factory Method is a creational design pattern that allows objects to be created without specifying the exact class of the object.
-> Allows multiple objects of different classes to be created
This is done by moving object initialisation inside a method, and using that method to get the object.
For example,
Changing
| |
to
| |
This way, we could also make the create method abstract, allowing subclasses to fill in the functionality
| |
