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
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(); } |
AbstractComputerFactory.java
1 2 3 4 5 6 7 8 9 10 |
package com.kkjavatutorials.factory; /** * AbstractComputerFactory shows one extra layer of abstraction * @author KK JavaTutorials */ public interface AbstractComputerFactory { public abstract Computer createComputer(); } |
ComputerFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.factory; /** * This ComputerFactory internally makes calls to * AbstractComputerFactory which shows one extra layer of abstraction * @author KK JavaTutorials */ public class ComputerFactory { private ComputerFactory() { } public static Computer getComputer(AbstractComputerFactory abstractComputerFactory) { return abstractComputerFactory.createComputer(); } } |
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 + "]"; } } |
LaptopFactory.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 |
package com.kkjavatutorials.factory; import com.kkjavatutorials.factory.impl.Laptop; /** *LaptopFactory implements AbstractComputerFactory and AbstractComputerFactory returns *reference of super interface Computer which shows one extra layer of abstraction. * @author KK JavaTutorials */ public class LaptopFactory implements AbstractComputerFactory { private String ram; private String hdd; private String cpu; private boolean isGraphicsEnabled; private boolean isBluetoothEnabled; public LaptopFactory(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; } /** * Overridden method of AbstractComputerFactory * which returns reference of super interface Computer */ @Override public Computer createComputer() { return new Laptop(ram, hdd, cpu, isGraphicsEnabled, isBluetoothEnabled); } } |
PCFactory.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 |
package com.kkjavatutorials.factory; import com.kkjavatutorials.factory.impl.PC; /** *PCFactory implements AbstractComputerFactory and AbstractComputerFactory returns *reference of super interface Computer which shows one extra layer of abstraction. * @author KK JavaTutorials */ public class PCFactory implements AbstractComputerFactory { private String ram; private String hdd; private String cpu; private boolean isGraphicsEnabled; private boolean isBluetoothEnabled; public PCFactory(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; } /** * Overridden method of AbstractComputerFactory * which returns reference of super interface Computer */ @Override public Computer createComputer() { return new PC(ram, hdd, cpu, isGraphicsEnabled, isBluetoothEnabled); } } |
ServerFactory.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 |
package com.kkjavatutorials.factory; import com.kkjavatutorials.factory.impl.Server; /** *ServerFactory implements AbstractComputerFactory and AbstractComputerFactory returns *reference of super interface Computer which shows one extra layer of abstraction. * @author KK JavaTutorials */ public class ServerFactory implements AbstractComputerFactory { private String ram; private String hdd; private String cpu; private boolean isGraphicsEnabled; private boolean isBluetoothEnabled; public ServerFactory(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; } /** * Overridden method of AbstractComputerFactory * which returns reference of super interface Computer */ @Override public Computer createComputer() { return new Server(ram, hdd, cpu, isGraphicsEnabled, isBluetoothEnabled); } } |
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 26 27 |
package com.kkjavatutorials.client; import com.kkjavatutorials.factory.Computer; import com.kkjavatutorials.factory.ComputerFactory; import com.kkjavatutorials.factory.LaptopFactory; import com.kkjavatutorials.factory.PCFactory; import com.kkjavatutorials.factory.ServerFactory; /** * @author KK JavaTutorials *Client program which makes use of Abstract Factory Design Pattern */ public class ClientTest { public static void main(String[] args) { //Client calls ComputerFactory to Create Computer with different configurations Computer pc = ComputerFactory.getComputer(new PCFactory("16 GB", "120 GB", "2,23 HGZ", true, true)); System.out.println("PC Config::"+pc); Computer server = ComputerFactory.getComputer(new ServerFactory("32 GB", "320 GB", "2,29 HGZ", true, true)); System.out.println("Server Config::"+server); Computer laptop = ComputerFactory.getComputer(new LaptopFactory("16 GB", "140 GB", "2,23 HGZ", true, true)); System.out.println("Laptop Config::"+laptop); } } |
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.