Sitemap

Factory Design Pattern

2 min readJul 5, 2023

--

We have many design patterns. Currently, we will discuss Factory.

Details

The Factory Pattern is a creational design pattern that provides an interface for creating objects but allows subclasses or implementing classes to decide which class to instantiate. It encapsulates the object creation logic within a separate factory class, providing a common interface for creating objects without exposing the instantiation logic to the client.

To understand the Factory Pattern, let’s consider an example of a vehicle manufacturing application. We have a Vehicle interface representing a generic vehicle, and we want to create different types of vehicles, such as cars and motorcycles.

Get Amit Dhiman’s stories in your inbox

Join Medium for free to get updates from this writer.

Define the Vehicle Interface:

public interface Vehicle {
void drive();
}

Implement Concrete Vehicle Classes:

public class Car implements Vehicle {
public void drive() {
System.out.println("Driving a car...");
}
}
public class Motorcycle implements Vehicle {
public void drive() {
System.out.println("Driving a motorcycle...");
}
}

Create a Vehicle Factory:

public class VehicleFactory {
public Vehicle createVehicle(String type) {
if (type.equalsIgnoreCase("car")) {
return new Car();
} else if (type.equalsIgnoreCase("motorcycle")) {
return new Motorcycle();
}
// Handle unknown vehicle types or return a default vehicle
return new Car();
}
}

In this example, the VehicleFactory class acts as the factory class responsible for creating the appropriate vehicle objects based on the given type.

Now, let’s see the usage of the factory:

VehicleFactory factory = new VehicleFactory();
Vehicle car = factory.createVehicle("car");
car.drive(); // Outputs: Driving a car...
Vehicle motorcycle = factory.createVehicle("motorcycle");
motorcycle.drive(); // Outputs: Driving a motorcycle...

In this example, we create a VehicleFactory object and use it to create instances of different vehicles by calling the createVehicle() method with the desired vehicle type. The factory internally decides which concrete class to instantiate based on the provided type and returns the corresponding object.

The Factory Pattern allows us to centralize the object creation logic in a factory class, providing a common interface (createVehicle() method) for clients to create objects without being aware of the specific implementation details. It promotes loose coupling between the client and the created objects, making it easier to maintain and extend the codebase.

The Factory Pattern is particularly useful in scenarios where object creation involves complex logic, requires additional configuration, or when there are multiple variations of object creation. It allows for flexibility and extensibility by abstracting the object creation process and decoupling the client from the specific implementation classes.

--

--

Amit Dhiman
Amit Dhiman

Written by Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS