Design Pattern: Factory Pattern in Java

Factory pattern helps to generate object by choosing one class from a bunch of classes. The class is chosen by some predefined criteria.

This article demonstrates Factory pattern implementations in Java. Check the following examples.

Examples

Here are few examples of Factory pattern implementation in Java-

Example #1: Transport

Let’s take the example of a transport system. Take a look at the class diagram.

Class Diagram

Factory Pattern - Transport Class Diagram

Transport Interface

  • Create an interface named “Transport”. This interface will be used by all item classes – “Bike”, “Car”, and “Plane”.
  • Declare some methods that need to be defined for all item classes. Here we have “start”, “stop”, and “repair” methods, as per our requirement.
// Transport.java

package com.bigboxcode.designpattern.factory.transport;

public interface Transport {
    void start();

    void stop();

    void repair();
}

Bike Class

  • Define item class “Bike”.
  • Implement “Transport” interface for “Bike”. Define “start”, “stop”, and “repair” methods for “Bike” class.
// Bike.java

package com.bigboxcode.designpattern.factory.transport;

public class Bike implements Transport {
    @Override
    public void start() {
        System.out.println("Bike started");
    }

    @Override
    public void stop() {
        System.out.println("Bike Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Bike Repair");
    }
}

Car Class

  • Define “Car” class.
  • Implement “Transport” interface for “Car”.
// Car.java

package com.bigboxcode.designpattern.factory.transport;

public class Car implements Transport {
    @Override
    public void start() {
        System.out.println("Car started");
    }

    @Override
    public void stop() {
        System.out.println("Car Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Car Repair");
    }
}

Plane Class

  • Define “Plane” class.
  • Implement “Transport” interface for “Plane”.
// Plane.java

package com.bigboxcode.designpattern.factory.transport;

public class Plane implements Transport {
    @Override
    public void start() {
        System.out.println("Plane started");
    }

    @Override
    public void stop() {
        System.out.println("Plane Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Plane Repair");
    }
}

Transport Factory Class

This factory class is responsible for generating transport objects. The getTransport method is used for object generation. This method takes a string as a param and returns an object of type Transport.

The param type and number for the method getTransport may vary in your case. And also the underlying logic of object generation might change based on your use case. Make any required changes in the method based on your requirement.

If you want, then you can declare the getTransport method as static.

  • Define “TransportFactory” class.
  • Define method “getTransport”. This method takes a string “type” as parameter, which is an string identifier. Then based on the provided “type” this method generates an object of the item classes (Bike, Car, or Plane), and returns that object.
// TransportFactory.java

package com.bigboxcode.designpattern.factory.transport;

public class TransportFactory {

    // Make this method static if you prefer
    public Transport getTransport(String type) {
        if (type == null) {
            return null;
        }

        if (type.equalsIgnoreCase("bike")) {
            return new Bike();
        }

        if (type.equalsIgnoreCase("car")) {
            return new Car();
        }

        if (type.equalsIgnoreCase("plane")) {
            return new Plane();
        }

        return null;
    }
}

Demo

  • Create an object of “TransportFactory”. Here we have saved it in “transportFactory” variable.
  • Now we can use the “TransportFactory” object to create item objects. Just pass the “type” (bike/car/plane) to the “getTransport” method, it will generate a new object and return that item object.
// Demo.java

package com.bigboxcode.designpattern.factory.transport;

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

        TransportFactory transportFactory = new TransportFactory();

        Transport transport1 = transportFactory.getTransport("bike");
        transport1.start();

        Transport transport2 = transportFactory.getTransport("car");
        transport2.start();

        Transport transport3 = transportFactory.getTransport("plane");
        transport3.start();

    }
}

Output

The demo code will generate the below output.

Bike started
Car started
Plane started

Example #2: Transport [Factory Method]

Here we are implementing the transport factory using the factory method. This implementation is different than the simple factory, as it uses an abstract class and separate factory classes for the factory implementation.

The item classes and item interface parts are the same as the simple factory. Just the factory implementation is different in this case.

Transport Interface

  • Create “Transport” interface.
  • Declare methods as per the requirement of item classes. Here we have “Start”, “stop”, and “repair” methods.
// Transport.java

package com.bigboxcode.designpattern.factory.transportmethod;

public interface Transport {
    void start();

    void stop();

    void repair();
}

Bus Concrete Item Class

  • Create item class “Bus”.
  • Implement “Transport” interface for “Bus” class. So, we have defined the “start”, “stop”, “repair” methods for “Bus” class.
// Bus.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class Bus implements Transport {
    @Override
    public void start() {
        System.out.println("Bus started");
    }

    @Override
    public void stop() {
        System.out.println("Bus Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Bus Repair");
    }
}

Bike Concrete Item Class

  • Create item class “Bike”.
  • Implement “Transport” interface for “Bike” class.
// Bike.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class Bike implements Transport {
    @Override
    public void start() {
        System.out.println("Bike started");
    }

    @Override
    public void stop() {
        System.out.println("Bike Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Bike Repair");
    }
}

Car Concrete Item Class

  • Create item class “Car”.
  • Implement “Transport” interface for “Car” class.
// Car.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class Car implements Transport {
    @Override
    public void start() {
        System.out.println("Car started");
    }

    @Override
    public void stop() {
        System.out.println("Car Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Car Repair");
    }
}

Plane Concreate Item Class

  • Create item class “Plane”.
  • Implement “Transport” interface for “Plane” class.
// Plane.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class Plane implements Transport {
    @Override
    public void start() {
        System.out.println("Plane started");
    }

    @Override
    public void stop() {
        System.out.println("Plane Stopped");
    }

    @Override
    public void repair() {
        System.out.println("Plane Repair");
    }
}

Helicopter Concrete Item Class

  • Create item class “Helicopter”.
  • Implement “Transport” interface for “Helicopter” class.
// Helicopter.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class Helicopter implements Transport {
        @Override
        public void start() {
            System.out.println("Helicopter started");
        }

        @Override
        public void stop() {
            System.out.println("Helicopter Stopped");
        }

        @Override
        public void repair() {
            System.out.println("Helicopter Repair");
        }
}

Transport Factory Abstract Class

// TransportFactory.java

package com.bigboxcode.designpattern.factory.transportmethod;

public abstract class TransportFactory {
    public void operateTransport(String name) {
        Transport transport = getTransport(name);

        transport.start();
        transport.stop();
    }

    public void repairTransport(String name) {
        Transport transport = getTransport(name);

        transport.repair();
    }

    public abstract Transport getTransport(String name);
}

Road Transport Factory

// RoadTransportFactory.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class RoadTransportFactory extends TransportFactory {
    @Override
    public Transport getTransport(String name) {
        if (name.equalsIgnoreCase("car")) {
            return new Car();
        }

        if (name.equalsIgnoreCase("bike")) {
            return new Bike();
        }

        if (name.equalsIgnoreCase("bus")) {
            return new Bus();
        }

        return null;
    }
}

Air Transport Factory

// AirTransportFactory.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class AirTransportFactory extends TransportFactory {
    @Override
    public Transport getTransport(String name) {
        if (name.equalsIgnoreCase("plane")) {
            return new Plane();
        }

        if (name.equalsIgnoreCase("helicopter")) {
            return new Helicopter();
        }

        return null;
    }
}

Demo

// Demo.java

package com.bigboxcode.designpattern.factory.transportmethod;

public class Demo {
    public static void main(String[] args) {
        TransportFactory roadTransportFactory = new RoadTransportFactory();
        TransportFactory airTransportFactory = new AirTransportFactory();

        roadTransportFactory.operateTransport("bus");

        System.out.println("---------------------------------------");

        airTransportFactory.operateTransport("helicopter");

        System.out.println("---------------------------------------");

        roadTransportFactory.repairTransport("bike");
    }
}

Output

Bus started
Bus Stopped
---------------------------------------
Helicopter started
Helicopter Stopped
---------------------------------------
Bike Repair

Source Code

Use the following link to get the source code:

Other Code Implementations

Use the following links to check Factory pattern implementation in other programming languages.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.