DEV Community

Cover image for Factory Design Pattern
Amrinder singh
Amrinder singh

Posted on • Updated on

Factory Design Pattern

What is it?
Factory Method is a creational design pattern that defines an interface or abstract class for creating an object, but allows subclasses to decide the type of objects that will be instantiated.

Problem
Let's say you own a Cab service company and provide cars as a mode of transport to travelers. Soon your service becomes famous and you want to expand your service to provide other modes of transportation say bikes. To do so you have to change your entire codebase to support bikes.

Solution
The Factory Method pattern suggests that instead of creating objects directly (using new operator) we use a factory method that creates an object for us. In our case instead of creating a new car object we let a factory method create an object for us and return the object we want car/bike

Class Diagram:

Example:

class Factory {
    public static void main(String[] args) {

        Transport t1 = CabService.getInstance().getCab("CAR");
        System.out.println(t1.getRate());

        Transport t2 = CabService.getInstance().getCab("BIKE");
        System.out.println(t2.getRate());

    }
}

class CabService{
    private static CabService onlyInstance;

    // Make the constructor private so its only accessible to
    // members of the class.
    private CabService() {
    }


    // Create a static method for object creation
    synchronized public static CabService getInstance() {

        if (onlyInstance == null) {
            // Only instantiate the object when needed.
            synchronized (CabService.class) {
                if (onlyInstance == null) {
                    onlyInstance = new CabService();
                }
            }
        }
        return onlyInstance;
    }

    public Transport getCab(String type) {
        return new CabFactory().getTransport(type);
    }
}

class CabFactory {

    //Factory method that returns transport based on type requested
    public Transport getTransport(String type) {
        switch(type) {
            case "CAR" :
                return new Car();
            case "BIKE" :
                return new Bike();
            default:
                return null;
        }
    }
}

//Transport abstract class
abstract class Transport {
    double rate = 0;
    public double getRate() {
        return rate;
    }
}

class Car extends Transport {
    double rate = 10;
    public double getRate() {
        return rate;
    }
}

class Bike extends Transport  {
    double rate = 5;
    public double getRate() {
        return rate;
    }
}

Usages:

  • 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 to its sub-classes.
  • when you want to provide users of your library or framework with a way to extend its internal components.
  • when you want to save system resources by reusing existing objects instead of rebuilding them each time

Advantages:

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code
  • Single Responsibility Principle
  • Open/Closed Principle

DisAdvantages:

  • The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern.

Github repo Link:
Factory Design Pattern Github

Top comments (0)