Exploring the Factory Design Pattern in Java- A Comprehensive Guide with Real-World Examples
What is Factory Design Pattern in Java with Example
The Factory Design Pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. It is also known as the Factory Method Pattern. This pattern is used when we want to create objects without specifying the exact class of object that will be created. Instead, we defer the instantiation to subclasses.
In Java, the Factory Design Pattern is widely used to encapsulate object creation logic and to decouple the client code from the creation process. This pattern is particularly useful when we have a large number of classes that share a common interface but have different implementations.
In this article, we will discuss the Factory Design Pattern in Java with an example. Let’s start by understanding the components of the Factory Design Pattern.
Components of Factory Design Pattern
1. Creator: This is an abstract class or interface that declares the factory method, which returns an object of a subclass.
2. Concrete Creator: This class implements the Creator class and overrides the factory method to instantiate and return an object of a specific subclass.
3. Product: This is an abstract class or interface that declares the type of product that can be created by the factory method.
4. Concrete Product: This class implements the Product interface and represents a specific type of product.
Example of Factory Design Pattern in Java
Let’s consider an example of a car manufacturing company. The company has different types of cars, such as Sedan, SUV, and Hatchback. We will create a Factory Design Pattern to encapsulate the object creation logic for these cars.
“`java
// Product interface
interface Car {
void drive();
}
// Concrete Product 1
class Sedan implements Car {
public void drive() {
System.out.println(“Driving a Sedan”);
}
}
// Concrete Product 2
class SUV implements Car {
public void drive() {
System.out.println(“Driving an SUV”);
}
}
// Concrete Product 3
class Hatchback implements Car {
public void drive() {
System.out.println(“Driving a Hatchback”);
}
}
// Creator interface
interface CarFactory {
Car createCar();
}
// Concrete Creator 1
class SedanFactory implements CarFactory {
public Car createCar() {
return new Sedan();
}
}
// Concrete Creator 2
class SUVFactory implements CarFactory {
public Car createCar() {
return new SUV();
}
}
// Concrete Creator 3
class HatchbackFactory implements CarFactory {
public Car createCar() {
return new Hatchback();
}
}
// Client code
public class FactoryPatternExample {
public static void main(String[] args) {
CarFactory sedanFactory = new SedanFactory();
Car sedan = sedanFactory.createCar();
sedan.drive();
CarFactory suvFactory = new SUVFactory();
Car suv = suvFactory.createCar();
suv.drive();
CarFactory hatchbackFactory = new HatchbackFactory();
Car hatchback = hatchbackFactory.createCar();
hatchback.drive();
}
}
“`
In this example, we have defined a `Car` interface and three concrete products (`Sedan`, `SUV`, and `Hatchback`). We also have a `CarFactory` interface and three concrete creators (`SedanFactory`, `SUVFactory`, and `HatchbackFactory`). The client code uses the factory methods to create and drive the cars without knowing the exact class of the car being created.
The Factory Design Pattern in Java provides a flexible and decoupled way to create objects, making it easier to manage the creation process and maintain the codebase.