Abstract Factory Design Pattern in Java

Fahim Foysal Emon
2 min readOct 13, 2021

--

Design pattern provides a general reusable solution for the common problems that occur in software design. The pattern typically shows relationships and interactions between classes or objects.

Hard-Coded is not a good idea of creating things. In this Creational Design Pattern we create new instances to by using new keywords.
Abstract Factory Pattern is one of the Creational Design Patterns.

The purpose of Abstract Factory Pattern is to provide an interface for creating families of related objects, without specifying concrete classes.

Wait, let me explain you a bit clearly…
Why do we need abstract factory pattern ?
Sometimes client does not need to know from which class he/she is getting the required data.
Since in abstract factory pattern it uses the generic information of the actual class.
That is why we use abstract factory pattern to actually hide the concrete classes from the client.

We will try to create a Design Pattern which will return currency name of specific countries without specifying concrete class names.

In Abstract Factory Pattern for every concrete class we create a factory class.
Here, for concrete class Bangladesh.java, we have created BangladeshFactory.java .

Bangladesh.java
BangladeshFactory.java

and then we have created a GlobalFactory.java and Client will use this GlobalFactory.java to get the concrete class.

GlobalFactory.java

Here our base interface is Global.java and all other concrete classes Bangladesh.java, America.java and India.java are implementing that interface. And these main concrete classes have the method of the currency name of it’s own.

Global.java
Bangladesh.java

In AbstractFactoryPackage.java , we have GlobalAbstractPayableFactory.java interface with a single method createPayable of type Global.java .

GlobalAbstractPayableFactory.java

So, now we have another three Factory Classes name BangladeshFactory.java, AmericaFactory.java and IndiaFactory.java like other three concrete classes.
We will now implement GlobalAbstractPayableFactory.java in BangladeshFactory.java, AmericaFactory.java and IndiaFactory.java classes to create Bangladesh.java, America.java and India.java class’s instances .
Where it will return corresponding concrete class’s instance.

Now let’s come to the last yet the most important part how client will get these instances ?

We will create a GlobalFactory.java class, where we will have a simple method name createPayable of type Global and the argument here will be GlobalAbstractPayableFactory.java type. So, when client will use createPayable method of GlobalFactory.java class they will actually pass the Factory Classes of concrete classes. like they will pass BangladeshFactory.java class instead of Bangladesh.java Class and it will return Bangladesh currency name.
and Client will get the desired value from the actual concrete class via factory class.

Main Class

https://github.com/emowzz/Payable_Abstract_Factory_Pattern

To see the actual code visit this link in github.

--

--