Abstract Factory Pattern in Java

By | July 4, 2020

In this post, We will talk and learn about the Abstract Factory Pattern in Java.

  • Abstract Factory pattern is almost similar to Factory Pattern except for the fact that it’s more like a factory of factories.
  • Abstract factory pattern we can consider as another layer of abstraction over factory pattern.
  • Whenever you need another level of abstraction over a group of factories, you should consider using the abstract factory pattern.

Abstract Factory Design Pattern Examples in JDK:

javax.xml.parsers.DocumentBuilderFactory#newInstance()
javax.xml.transform.TransformerFactory#newInstance()
javax.xml.xpath.XPathFactory#newInstance()

Now Let’s move towards implementation of the Abstract Factory design pattern in java.

Below is the complete source code:

Abstract Factory Design Pattern Project

Computer.java

AbstractComputerFactory.java 

ComputerFactory.java

Laptop.java

PC.java

Server.java

LaptopFactory.java 

PCFactory.java

ServerFactory.java

Finally, We have a Client program ClientTest.java

Output of this client program:

PC Config::Laptop [ram=16 GB, hdd=120 GB, cpu=2,23 HGZ, isGraphicsEnabled=true, isBluetoothEnabled=true]
Server Config::Laptop [ram=32 GB, hdd=320 GB, cpu=2,29 HGZ, isGraphicsEnabled=true, isBluetoothEnabled=true]
Laptop Config::Laptop [ram=16 GB, hdd=140 GB, cpu=2,23 HGZ, isGraphicsEnabled=true, isBluetoothEnabled=true]

You May Also Like:

Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java

That’s all about the Abstract Factory Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *