factory method pattern diagram

Factory Method Design Pattern – How Factory Works

Factory method design pattern is a creational design pattern that uses factory methods to deal with creating instances without specifying the exact class of the object that will be created. Before you go to example, let us try to understand how factory method pattern works.

How factory method pattern works ?

Let’s look at the factory pattern diagram below

factory method design pattern

In this figure, A is the super class or base class. And classes A1 and A2 are derived classes or sub classes derived from A. The Factory is a class that decides which of these sub classes to be returned depending on the arguments you provide to getA() method.

The getA() method is the factory method that accepts some value type, and returns some instances of the class A and which one it returns does not matter to programmer.

Now let’s see how to do sample program for factory method pattern.

Factory Method Pattern – Super Class

Super class in the factory method pattern can be an interface or abstract class or normal class. For our example we have created abstract super class Vehicle.java with method toString() to print the properties of the object that is created and returned by factory.

package com.sneppets.java.dp.factory;

/**
 * A - abstract base class or super class
 * @author sneppets.com 
 */

public abstract class Vehicle {
	
	public abstract String getColor();
	
	public abstract int getWheels();
	
	public String toString()
	{
		return " Color - " + this.getColor() + ", Wheels - " + this.getWheels();
	}	

}

Sub Classes – Factory Pattern

Let’s create two sub classes Bike and Car as shown below

package com.sneppets.java.dp.factory;

/**
 * A1 - sub class 1 from A
 * @author sneppets.com
 */
public class Bike extends Vehicle{
	
	private String color;
	
	private int wheels;
	
	public Bike(String color) {
		this.setColor(color);
		this.setWheels(2);
		System.out.println("Created New Bike");
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getWheels() {
		return wheels;
	}

	public void setWheels(int wheels) {
		this.wheels = wheels;
	}

}

Note, both classes should extend Vehicle (Super class)

package com.sneppets.java.dp.factory;

/**
 * A2 - sub class 2 from A
 * @author sneppets.com
 */
public class Car extends Vehicle{
	
	private String color;
	
	private int wheels;

	public Car(String color) {
		this.setColor(color);
		this.setWheels(4);
		System.out.println("Created New Car");
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	
	public int getWheels() {
		return wheels;
	}

	public void setWheels(int wheels) {
		this.wheels = wheels;
	}
}

Factory Class

We are done with super class and sub classes. Now let’s write our Factory class VehicleFactory.java with factory method getVehicle() as shown below

package com.sneppets.java.dp.factory;

/**
 * Factory class
 * getVehicle() is the factory method
 * @author sneppets.com
 */
public class VehicleFactory {

	Vehicle vehicle;
	
	public static Vehicle getVehicle(String vehicleType, String color) {
		
		if(vehicleType == "type1")
		{
			return new Bike(color);
		}
		else if(vehicleType == "type2")
		{
			return new Car(color);
		}
		return null;
	}
}

Note: You can keep Factory class as Singleton class or we can write static method like shown above.

Below is our implementation diagram for your understanding

factory method design pattern diagram

Factory method design pattern – Demo

Here is our simple test program to demo factory method pattern

package com.sneppets.java.dp.factory;

/**
 * Program to demonstrate Factory Design Pattern
 * @author sneppets.com
 */
public class FactoryDemo {
	
	public static void main(String[] args)
	{
		//factory method returns some instance of class A (Bike) based on type
		Vehicle vehicle1 = VehicleFactory.getVehicle("type1","red");
		System.out.println("Vehicle 1 :" + vehicle1.toString());	
		
		//factory method returns some instance of class A (Car) based on type
		Vehicle vehicle2 = VehicleFactory.getVehicle("type2","blue");
		System.out.println("Vehicle 2 :" + vehicle2.toString());		
		
	}

}

Output

Created New Bike
Vehicle 1 : Color - red, Wheels - 2
Created New Car
Vehicle 2 : Color - blue, Wheels - 4

When to use Factory Method Pattern ?

You should consider to use Factory Method Pattern

  • When a class can’t expect what kind of class objects it must create
  • Whenever a class uses its sub classes to specify which objects it creates.

Related Posts

Reference

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments