By Definition, Abstract Factory is “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.“. This simply means that this design pattern is made to hide the actual derived class and create a new layer (or wrapper) that will create the actual instance of this classes. The groupings of classes will be based on their relationship and commonality among its features.
I create a Java code that will showcase the design patterns benefit. The example I made is based on a real world scenario where person types are created for the factory business that needs them.
Download the code: here
Checking at the code. The PeopleFactory is an abstract “wrapper” class created to host the creation of Person Objects. As seen, the PeopleFactory provides the layer of security as it hides the actual class implementation / initialization.
My Cent: Abstract Factory do protect the actual implementation / derived class by encapsulating from the other layers of the application. Yet It does have one disadvantage in terms of flexibility: Its made of an abstract class. As every developer should know, abstract class are also extensions of derived class – inheritance of a derived class. OOP only supports single inheritance and that makes the actual factory class not flexible enough.
What do you think is the best replacement for an Abstract class in this case?