Factory design pattern in java

By | July 4, 2020

In this post, We will talk and learn about the Factory design pattern in java.

Factory pattern comes under a creational design pattern. It is one of the most used design patterns in Java. Using this design pattern we can create an object without exposing the object creation logic to the client and refer to the newly created object using a common interface.

Factory Design Pattern Examples in JDK:

java.util.Calendar and java.text.NumberFormat getInstance() method
Java.util.ResourceBundle getBundle() method.
java.nio.charset.Charset forName() method
java.util.EnumSet of() method
java.sql.DriverManager getConnection() method
java.net.URL openConnection() method
java.lang.Class newInstance() method
java.lang.Class forName() method

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

Below is the complete source code:

Factory Design Pattern ProjectSuper Interface Computer.java

Now We have three(Laptop.java,PC.java and Server.java) Implementation of above super interface

Laptop.java

PC.java

Server.java

ComputerType.java

ComputerFactory.java 

Finally, We have a client program ClientTest.java

Output of this 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

That’s all about Factory design 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 *