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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkjavatutorials.factory; /** * This Super type can be implemented by many classes * This may be abstract class as well * @author KK JavaTutorials */ public interface Computer { public abstract String ram(); public abstract String hdd(); public abstract String cpu(); public boolean isGraphicsEnabled(); public boolean isBluetoothEnabled(); } |
Now We have three(Laptop.java,PC.java and Server.java) Implementation of above super interface
Laptop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.kkjavatutorials.factory.impl; import com.kkjavatutorials.factory.Computer; /** * One of implementation of Computer(Super Type) * @author KK JavaTutorials */ public class Laptop implements Computer { private String ram; private String hdd; private String cpu; private boolean isGraphicsEnabled; private boolean isBluetoothEnabled; public Laptop(String ram, String hdd, String cpu, boolean isGraphicsEnabled, boolean isBluetoothEnabled) { super(); this.ram = ram; this.hdd = hdd; this.cpu = cpu; this.isGraphicsEnabled = isGraphicsEnabled; this.isBluetoothEnabled = isBluetoothEnabled; } @Override public String ram() { return this.ram; } @Override public String hdd() { return this.hdd; } @Override public String cpu() { return this.cpu; } @Override public boolean isGraphicsEnabled() { return this.isGraphicsEnabled; } @Override public boolean isBluetoothEnabled() { return this.isBluetoothEnabled; } @Override public String toString() { return "Laptop [ram=" + ram + ", hdd=" + hdd + ", cpu=" + cpu + ", isGraphicsEnabled=" + isGraphicsEnabled + ", isBluetoothEnabled=" + isBluetoothEnabled + "]"; } } |
PC.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kkjavatutorials.factory.impl; import com.kkjavatutorials.factory.Computer; /** * One of implementation of Computer(Super Type) * @author KK JavaTutorials */ public class PC implements Computer { private String ram; private String hdd; private String cpu; private boolean isGraphicsEnabled; private boolean isBluetoothEnabled; public PC(String ram, String hdd, String cpu, boolean isGraphicsEnabled, boolean isBluetoothEnabled) { super(); this.ram = ram; this.hdd = hdd; this.cpu = cpu; this.isGraphicsEnabled = isGraphicsEnabled; this.isBluetoothEnabled = isBluetoothEnabled; } @Override public String ram() { return this.ram; } @Override public String hdd() { return this.hdd; } @Override public String cpu() { return this.cpu; } @Override public boolean isGraphicsEnabled() { return this.isGraphicsEnabled; } @Override public boolean isBluetoothEnabled() { return this.isBluetoothEnabled; } @Override public String toString() { return "Laptop [ram=" + ram + ", hdd=" + hdd + ", cpu=" + cpu + ", isGraphicsEnabled=" + isGraphicsEnabled + ", isBluetoothEnabled=" + isBluetoothEnabled + "]"; } } |
Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kkjavatutorials.factory.impl; import com.kkjavatutorials.factory.Computer; /** * One of implementation of Computer(Super Type) * @author KK JavaTutorials */ public class Server implements Computer { private String ram; private String hdd; private String cpu; private boolean isGraphicsEnabled; private boolean isBluetoothEnabled; public Server(String ram, String hdd, String cpu, boolean isGraphicsEnabled, boolean isBluetoothEnabled) { super(); this.ram = ram; this.hdd = hdd; this.cpu = cpu; this.isGraphicsEnabled = isGraphicsEnabled; this.isBluetoothEnabled = isBluetoothEnabled; } @Override public String ram() { return this.ram; } @Override public String hdd() { return this.hdd; } @Override public String cpu() { return this.cpu; } @Override public boolean isGraphicsEnabled() { return this.isGraphicsEnabled; } @Override public boolean isBluetoothEnabled() { return this.isBluetoothEnabled; } @Override public String toString() { return "Laptop [ram=" + ram + ", hdd=" + hdd + ", cpu=" + cpu + ", isGraphicsEnabled=" + isGraphicsEnabled + ", isBluetoothEnabled=" + isBluetoothEnabled + "]"; } } |
ComputerType.java
1 2 3 4 5 6 7 8 9 10 |
package com.kkjavatutorials.factory; /** * Types of Computers Created by ComputerFactory * @author KK JavaTutorials */ public enum ComputerType { PC,SERVER,LAPTOP } |
ComputerFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.kkjavatutorials.factory; import com.kkjavatutorials.factory.impl.Laptop; import com.kkjavatutorials.factory.impl.PC; import com.kkjavatutorials.factory.impl.Server; /** * This Factory class returns Computer instance as per * provided input parameters * @author KK JavaTutorials */ public class ComputerFactory { private ComputerFactory() { } public static Computer getComputer(ComputerType computerType,String ram,String hdd,String cpu,boolean isGraphicsEnabled,boolean isBluetoothEnabled) { switch (computerType) { case PC: return new PC(ram, hdd, cpu, isGraphicsEnabled, isBluetoothEnabled); case SERVER: return new Server(ram, hdd, cpu, isGraphicsEnabled, isBluetoothEnabled); case LAPTOP: return new Laptop(ram, hdd, cpu, isGraphicsEnabled, isBluetoothEnabled); default: throw new RuntimeException("Invalid Computer Type!!"); } } } |
Finally, We have a client program ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kkjavatutorials.client; import com.kkjavatutorials.factory.Computer; import com.kkjavatutorials.factory.ComputerFactory; import com.kkjavatutorials.factory.ComputerType; /** * @author KK JavaTutorials *Client program which makes use of Factory Design Pattern */ public class ClientTest { public static void main(String[] args) { //Create Computer with different configurations Computer pc = ComputerFactory.getComputer(ComputerType.PC, "16 GB", "120 GB", "2,23 HGZ", true, true); System.out.println("PC Config::"+pc); Computer server = ComputerFactory.getComputer(ComputerType.SERVER, "32 GB", "320 GB", "2,29 HGZ", true, true); System.out.println("Server Config::"+server); Computer laptop = ComputerFactory.getComputer(ComputerType.LAPTOP, "16 GB", "140 GB", "2,23 HGZ", true, true); System.out.println("Laptop Config::"+laptop); } } |
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.