Abstract Factory
The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.
Frameworks that are providing dependency injection, like Spring, are solving this issue and probably, we won't needed Abstract Factory there.
Example - Animal factory
Each domain object (like Cat and Dog) are going to have it's own factory. That way, can would never have to call new Cat()
or new Doc()
constructors. Instead we tell the factory the inputs and the factory makes sure we get an instance.
This way, we can easily change Cat and Dog implementations without affecting user of our factories.
AnimalAbstractFactory
can be implemented as interface or abstract class, it depends what is more useful for your problem.
Then we need to create an object, we create specific factory and as a user of such code, we no idea what instance is going to be created (because probably we shouldn't care).
Last updated
Was this helpful?