The Factory Method Design Pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In Java, this pattern is commonly used to abstract the creation of objects and to provide a way to create objects without exposing the creation logic to the client.
The Factory Method Design Pattern is used in situations where the client needs to create objects of various types, but does not know how to create them. By providing a Factory Method, we can abstract the creation of objects and allow the client to create objects without knowing the specific implementation details.
Implementation of Factory Method Pattern in Java:
To implement the Factory Method pattern in Java, we need to follow a few basic steps. These are:
Superclass: We need to create a superclass that defines a method for creating objects.
Concrete Classes: We need to create concrete classes that implement the Factory Method defined in the superclass.
Client Code: We need to write client code that uses the Factory Method to create objects.
In the implementation example below, we create a superclass called Vehicle that defines an abstract method called drive(). We then create two concrete classes, Car and Truck, that implement the drive() method.
Next, we create a VehicleFactory class that defines a static method called createVehicle(). This method takes a vehicleType parameter and returns an instance of the corresponding concrete class. In the createVehicle() method, we use a conditional statement to determine the type of vehicle to create.
Finally, in the client code, we use the createVehicle() method to create objects of the Car and Truck classes, and then call the drive() method on each object.
Here is the example implementation:
// Superclass
public abstract class Vehicle {
public abstract void drive();
}
// Concrete Classes
public class Car extends Vehicle {
public void drive() {
System.out.println("Driving a car…");
}
}
public class Truck extends Vehicle {
public void drive() {
System.out.println("Driving a truck…");
}
}
// Factory Class
public class VehicleFactory {
public static Vehicle createVehicle(String vehicleType) {
if(vehicleType.equals("car")) {
return new Car();
} else if(vehicleType.equals("truck")) {
return new Truck();
} else {
return null;
}
}
}
// Client Code
public class Client {
public static void main(String[] args) {
Vehicle car = VehicleFactory.createVehicle("car");
Vehicle truck = VehicleFactory.createVehicle("truck");
car.drive(); // Driving a car…
truck.drive(); // Driving a truck…
}
}
We create a VehicleFactory class that defines a static method createVehicle() which takes a vehicleType parameter and returns an instance of the corresponding concrete class. In the createVehicle() method, we use a conditional statement to determine the type of vehicle to create.
In the client code, we use the createVehicle() method to create objects of Car and Truck classes, and then call the drive() method on each object.
Advantages of Factory Method Pattern:
Abstraction: The Factory Method pattern provides an abstraction layer between the client code and the creation of objects, making it easy to modify the creation logic without affecting the client code.
Flexibility: The Factory Method pattern allows subclasses to alter the type of objects that will be created, providing flexibility in object creation.
Encapsulation: The Factory Method pattern encapsulates the object creation process, making it easier to manage and maintain the code.
Reusability: The Factory Method pattern promotes code reuse by providing a way to create objects without duplicating code.
Disadvantages of Factory Method Pattern:
Complexity: The Factory Method pattern can add complexity to the code, especially if there are many subclasses or if the creation logic is complex.
Overhead: The Factory Method pattern can add overhead to the code, as it requires the creation of additional classes and methods.
Pingback: GoF (Gang of Four) Design Pattern - Java To The Point