Abstract Factory Design Pattern
Abstract Factory design pattern adds a layer of abstraction on top of Factory design pattern. It is also known as factory of factories.
Using abstract factory class, first we get the specialized factory, and once we have it, we can get the product made by that factory. In this example, we are going to have an ElectronicDeviceFactory which can give us the objects of ComputerFactory and MobileFactory. Once, we get the factory object, it functions exactly as factory design pattern.
Computer.java
package com.sks;
public abstract class Computer {
protected String OS;
protected String brand;
//getters and setters, constructors, toString
}
Desktop.java
package com.sks;
public class Desktop extends Computer {
private String keyboardType;
//getters and setters, constructors, toString
}
Laptop.java
package com.sks;
public class Laptop extends Computer {
private boolean isMouseProvided;
//getters and setters, constructors, toString
}
Mobile.java
package com.sks;
public abstract class Mobile {
protected String os;
protected int price;
//getters and setters, constructors, toString
}
Samsung.java
package com.sks;
public class Samsung extends Mobile {
private String androidVersion;
//getters and setters, constructors, toString
}
Apple.java
package com.sks;
public class Apple extends Mobile {
private String phoneVersion;
//getters and setters, constructors, toString
}
Factory classes/interface
ElectronicDeviceFactory interface
package com.sks;
public interface ElectronicDeviceFactory {
public Object getProduct(String productType);
}
ComputerFactory.java
package com.sks;
public class ComputerFactory implements ElectronicDeviceFactory {
@Override
public Object getProduct(String type) {
Computer c = null;
switch (type) {
case "Desktop":
c = new Desktop("Windows", "HP", "querty");
break;
case "Laptop":
c = new Laptop("Windows", "HP", true);
}
if(c == null) {
throw new RuntimeException("Please provide a valid input");
}
return c;
}
}
MobileFactory.java
package com.sks;
public class MobileFactory implements ElectronicDeviceFactory {
@Override
public Object getProduct(String type) {
Mobile c = null;
switch (type) {
case "Apple":
c = new Apple("iOS", 80000, "iphone16");
break;
case "Samsung":
c = new Samsung("Android", 82000, "Android14");
}
if(c == null) {
throw new RuntimeException("Please provide a valid input: Apple or Samsung");
}
return c;
}
}
FactoryCreator.java
package com.sks;
public class FactoryCreator {
public static ElectronicDeviceFactory getFactory(String type) {
ElectronicDeviceFactory f = null;
switch (type) {
case "Computer":
f = new ComputerFactory();
break;
case "Mobile":
f = new MobileFactory();
}
return f;
}
}
Test class
package com.sks;
public class AbstractFactoryTest {
public static void main(String[] args) {
ElectronicDeviceFactory mobileFactory = FactoryCreator.getFactory("Mobile");
Mobile m = (Mobile)mobileFactory.getProduct("Samsung");
System.out.println(m);
ElectronicDeviceFactory computerFactory = FactoryCreator.getFactory("Computer");
Computer c = (Computer)computerFactory.getProduct("Desktop");
System.out.println(c);
}
}
Output: