Factory Method Pattern
Source:: Software Design Patterns - Best Practices for Software Developers
Definition
Provide an interface for object creation but delegate the actual instantiation of objects to subclasses.
This makes an instance of several derived classes based on interfaced data or events.
A factory method may or may not provide a default or generic implementation but lets subclasses specialize or modify the product by overriding the create/make methods.
When to Use
- When our object or component setup involves a high level of complexity
- When we need to easily generate different instances of objects depending on the environment we are in
- Where we’re working with many small objects or components that share the same properties
- When composing objects with instances of other objects that need to only satisfy an API contract to work.