- Intent ⇒ provides an interface for creating objects in a superclass (sharing methods —posibbly not all) while allowing for subclasses to alter the type of objects that will be created. Separting the what from the how.
- Very useful for rapid extensibility (i.e., PnP ⇒ plug and play)
- Logic
- A single object creator interface with some general methods
- The responsibility here is to do some business logic the relies on the Produc objects retured by the factory method
- Common business logic agnostic to the type of product
- HERE is where you call the product-specific methods (after instantiang the factory method)
- Factory method is an abstractmethod whose purpose is only to overwrite the given object to be created
- They instantiate a new object
- The final concrete object has some unified interface methods
- Pros and cons
- Pros ⇒
- avoid tight coupling
- single responsibility principle ⇒ concentrating object creation in a single palce
- open-closesd principle (OCP) ⇒ open for extension, closed for modification (existing shared-logic modules—parent classes)
- Client usage
- The client only instantiates the
ConcreteCreator
+ calls the Creator
methods
- Code
- Use
abstractmethod
and ABC
from abc
library
- Example