English English

Java Pattern Factory - Encapsulate the creation of an Class instance

The pattern "Factory" can be used in cases where the creation process of an object instance of a class is too complicated or should be separated.

In this pattern there is an interface which is implemented by sub classes that are used by a factory class (in this example: "TruckFactory"). If you want to create a new instance, then you have to call a function (in this example: "getTruck(String truckManifacturer)" in the instance of your factory class, which will return you your new object instance. The new object instance will be referenced by the super class that is the interface. 

A call to create an object instance would like this:

MyFactory myFactory = new MyFactory();
MyObjectSuperClass obj1 = myFactory.getFactoryInstance("MyObjectSubClass");

You pass the name of your class in the method of the factory class, instead of creating it by the command "new".

 

UML diagram

The structure of the application that will be created for this "Factory" pattern tutorial. A truck has the function "start", which implemented differently by the truck manufactures.
The factory class "TruckFactory" is used to get a new Truck instances for a certain truck manufactures.

 

Example

This is an example application which creates and runs different truck manufactures.

public interface Truck {
	void start();
}
public class Volvo implements Truck {
	@Override
	public void start() {
		System.out.println("Volvo truck was started.");
	}

}
public class Man implements Truck {
	@Override
	public void start() {
		System.out.println("Man truck was started.");
	}
}
public class TruckFactory {

	public Truck getTruck(String truckManufacturer) {
		if(truckManufacturer != null) {
			if(truckManufacturer.equalsIgnoreCase("Volvo")) {
				return new Volvo();
			} else if (truckManufacturer.equalsIgnoreCase("Man")) {
				return new Man();
			}
		} 
		return null;
	}
	
}
public class TruckFactoryPattern {

	public static void main(String[] args) {
		TruckFactory truckFactory = new TruckFactory();

		Truck manObj = truckFactory.getTruck("Man");
		Truck volvoObj = truckFactory.getTruck("Volvo");

		manObj.start();
		volvoObj.start();
	}

}

Output:

Man truck was started.
Volvo truck was started.

 

Other types of the pattern "Factory"

Factory method
This method is used to create an instance of the class from where this method itself is called of. A factory method is always an abstract method.

Abstract factory
This is an extension of the the implementation of the factory pattern and it is used to create several object types which still have a relation with each other.
There is an abstract factory class which is extended by concrete factory classes (sub class). The concrete factory classes implement the method or methods which are defined in the super class (the abstract factory class).
These concrete factory classes create instances of concrete classes. The concrete classes contain the implemented methods of their respective abstract super classes.

 

This tutorial as a application

Github repos: https://github.com/a-dridi/factory-TruckFactoryDemo

 

We use cookies on our website. They are essential for the operation of the site
Ok