Introduction
In today's tutorial, we will see the use of a simple factory in Java and how it's different from the abstract factory pattern.The simple factory pattern is a creational design pattern that provides a centralized way to create objects without exposing the underlying logic and complexity of object creation.
The simple factory and factory pattern are both different patterns that mainly focus on the way an object is created. In the factory method design pattern, the super, base class, or interface specifies a method for creating objects but also allows the subclasses to override the method and alter object creation logic.
What is the simple factory design pattern
The simple factory design pattern, which is one of the creational design patterns, is not considered as a design pattern in gangs of four (GOF), This pattern solves the problem of object instantiation based on simple criteria. For example, in the below code, we are creating different objects for Employee based on the enum value.In a simple factory pattern, we move the above instantiation logic to a separate class, commonly a static method.
publicstaticEmployeegetEmployeeInstance(EmployeeEnumtype) {
if (type.equals(EmployeeEnum.FULLTIMEEMPLOYEE)) {
returnnewFullTimeEmployee(12, "Pater", newAddress("StoneleighPlace", 600024),
1000, 100);
}
elseif(type.equals(EmployeeEnum.PARTTIMEEMPLOYEE)) {
returnnewPartTimeEmployee(13, "John", newAddress("BuckfastStreet", 600033), 20,
"6M");
} else {
returnnull;
}
}
Why simple factory pattern is not considered a design pattern in GOF
A simple factory pattern is not considered a design pattern in GOF as the pattern does not solve recurring design problems as it simply encapsulates the object instantiate logic into a method, mostly a static method in a separate concrete class.Also, the pattern must have a well-defined structure that describes the relationships between the components involved. The Simple Factory pattern does not have a specific structure or set of relationships, as it can be implemented in different ways depending on the specific requirements of the application.
Implementing the factory design pattern in Java
We are going to use the previous example of Employee inheritance in the prototype design pattern. The UML diagram below shows the overall structure of the class hierarchy.1. Create an interface or abstract class that defines the behavior of the object that will be created by the factory. For example, here the Employee class is the base class, FullTimeEmployee and PartTimeEmployee are the child classes.
2. Create a concrete class that implements the interface or abstract class. Here we implement the base class (Employee).
3. Create a factory class with a static method that contains object creation logic and returns objects.
public class SimpleFactoryEmployee {
public static Employee getEmployeeInstance(EmployeeEnum type) {
return switch (type) {
case FULLTIMEEMPLOYEE -> new FullTimeEmployee(12, "Pater", new Address("Stoneleigh Place", 600024),
1000, 100);
case PARTTIMEEMPLOYEE -> new PartTimeEmployee(13, "John", new Address("Buckfast Street", 600033), 20,
"6M");
default ->
throw new IllegalArgumentException("Unsupported type: " + type);
};
}
}
The method takes an enum value which is used to instantiate an appropriate concrete class. In a few cases, we can pass the additional parameters to the method which will be used during the creation of new objects.
Here we are using a switch statement which takes the enum value, based on the enum we create an object and return it. This is part of using the simple factory design.
public enum EmployeeEnum {
FULLTIMEEMPLOYEE,
PARTTIMEEMPLOYEE
}
Let's create client code that interacts with our factory method to get the object. The client does not need to know about the specific implementation logic.
public class Client {
public static void main(String[] args) {
try {
printEmployee(EmployeeEnum.FULLTIMEEMPLOYEE);
printEmployee(EmployeeEnum.PARTTIMEEMPLOYEE);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void printEmployee(EmployeeEnum employeeEnum) {
Employee employee = SimpleFactoryEmployee.getEmployeeInstance(employeeEnum);
System.out.println(employee);
}
}
Difference between Simple factory and factory method
Simple Factory |
Factory method |
Simple factory method Move the code responsible for creating
objects to a separate class with a static method. |
The factory method pattern is used when we delegate object
creation to subclasses. |
The client knows the object it is going to create. |
The client does not know the object in advance. |