top of page
Writer's pictureThe Tech Platform

Factory Design Pattern in Java

Updated: Apr 5, 2023

A Design Pattern provides a simple solution to common problems that are faced in day-to-day life by software developers. Three basic types of design patterns exist, which are listed below:

  • Creational Pattern

  • Structural Pattern

  • Behavioural Pattern

The pattern described here is like a blueprint that you can customize to solve a particular design problem and they only differ by complexity and scalability of the code.

So here in this article, we will learn about the Factory design pattern. Let’s get started.

Factory Design Pattern

Factory Design Pattern is a part of the creational design pattern. Also known as Virtual Constructor. The Factory Design Pattern in object-oriented programming that allows the creation of objects without specifying the exact class of object that will be created. Instead, a factory method is used to create objects based on a given set of parameters. In the Software Development Life Cycle (SDLC) or Software Engineering (SE), whenever working on a project, we have to work with models and all these models have loose coupling & high cohesion. So to achieve loose coupling, we use something called factory i.e instead of manually creating the object, ask someone else to create the object.

Let's understand this example practically:


STEP 1: First, create an interface or abstract class for the product that will be created by the factory. For example, we can create an interface named Vehicle:

public interface Vehicle {
    void drive();
}

STEP 2: Create concrete classes that implement the Vehicle interface. For example, we can create two classes named Car and Motorbike:

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

public class Motorbike implements Vehicle {
    @Overridepublic void drive() {
        System.out.println("Driving a motorbike");
    }
}

STEP 3: Create a factory class that will create objects of the Vehicle interface. For example, we can create a class named VehicleFactory:

public class VehicleFactory {
    public static Vehicle createVehicle(String type) {
        if (type.equalsIgnoreCase("car")) {
            return new Car();
        } else if (type.equalsIgnoreCase("motorbike")) {
            return new Motorbike();
        } else {
            throw new IllegalArgumentException("Unknown vehicle type");
        }
    }
}

STEP 4: Finally, we can use the VehicleFactory to create objects of the Vehicle interface. For example:

Vehicle car = VehicleFactory.createVehicle("car");
car.drive(); // Output: Driving a car

Vehicle motorbike = VehicleFactory.createVehicle("motorbike");
motorbike.drive(); // Output: Driving a motorbike

In this example, we have created a factory method createVehicle in the VehicleFactory class that returns an object of the Vehicle interface based on the input type parameter. This allows us to create objects of the Vehicle interface without knowing the exact class of the object that will be created.


When to use Factory Design Pattern:

  • When a class doesn't know what sub-classes will be required to create

  • When a class wants that its sub-classes specify the objects to be created.

  • When the parent classes choose the creation of objects for its sub-classes.

Advantages of Factory Design Pattern:

  • Allow clients to reuse the same object creation code across different parts of an application

  • Help to encapsulate object creation logic and allows clients to create objects without having to know the implementation details of the objects.

  • Provides a consistent interface for creating objects, which can simplify client code and make it easier to understand.

Conclusion

The Factory Design Pattern is a powerful tool for creating objects in Java that can help simplify and streamline application development. By encapsulating object creation logic and promoting code reuse, the Factory Design Pattern can help improve the quality, flexibility, and maintainability of Java code.


0 comments

Comments


bottom of page