Creational – Abstract Factory Design Pattern
July 27, 2020 1 min read

Abstract Factory Pattern means defining an interface or abstract class for creating families of related objects but without specifying there concrete sub classes. That means a class returns a factory of classes. Abstract Factory pattern is also known as Kit.
Let’s go with a real world example.

First I created an interface called “Shape”. There you can see a method called draw(). Next I created three concrete classes “Square”, “Circle” and “Cube” that implement the Shape Interface in the lines 1, 2 and 3. Inside those classes the method that created in the interface are overridden. Let’s go to the next step.

Here I created an abstract class called “AbstractFactory” to get the factories for Square, Circle and Cube Objects. In that class you can see the method getShape() in the type “Shape”. In lines 4 and 5 I created the factory classes “TwoDShape” and “ThreeDShape” by extending the abstract class “AbstractFactory” to generate the objects of concrete classes based on given information. See the next step below.

Next I created a class called “FactoryCreator” to get the factories by passing an information such as square, circle or cube. Let’s see the main method.

In the main method, I used the “FactoryCreator” class to get the “AbstractFactory” in order to get factories of concrete classes by passing an information such as type of shape( 2D or 3D). Try this example and see what is coming as the output.
The main difference between “Factory Method” and “Abstract Factory” is that the factory method is a single method (used to create one product) and an abstract factory is an object (creating families of related products).
You can take a look around Factory Method using the link given below.
Thank you.


