In Abstract Factory Pattern the major entities involved are
- Abstract Base Factory
- Concrete Factories each extending Abstract base factory
- Client code
- Base product
- Concrete product classes extending Base product
I have seen at various implementations and observed that the Client code knows about the concrete factories. As per the common definition of the pattern I have seen at various places is as below
Defines an interface for creating objects, but let sub-classes to decide which class to instantiate.
As per my observation, createProduct
is implemented as abstract method in abstract factory method. It exposes a non abstract public method like getProduct
from where it makes call to the createProduct
method. As per the run-time object of concrete factory class createProduct
is called accordingly.
Even if the base abstract factory class is not present then client code can simply call the createProduct
methods on the object of concrete factory classes as concrete factory classes are visible to client code.
I feel Abstract base class is only useful in below case
If we have code to create the objects of the concrete Factory classes separate from the code calling the getProduct
method on those objects. Code instantiating the concrete Factory classes
can
put the factories in some queue and later queue can be iterated and
Concrete products
can be obtained.
Please provide your valuable feedback.