Factory Design Pattern Behavior

What is the Factory pattern?

The Factory pattern is used to create an object without exposing the creation logic to the client and it creates the new object from a common interface. The factory design pattern in java

The factory design pattern is useful when we have an interface (Java interface or abstract class) and multiple sub-classes. We can create an object of subclasses based on input. The factory design pattern says just define an interface or an abstract class and the subclasses will decide which object should be created. It means subclasses will be responsible to create the object of the class according to the use of the object. The factory provides a method known as the Factory method and it allows a class to defer instantiation to subclasses. This Factory Method Pattern is also known as Virtual Constructor.

Why we use factory pattern java?

When we develop an application or project, we write a huge code that maybe leads to coupling problems. When the code gets larger, we face real-time problems. If a class needs any extra feature, then we figure all the code where we are using the class objects or method.

Suppose if we have an interface Car that is implemented by Toyota class, Nissan class and Honda class. Each Car class has a constructor that is used some set of configurations to create a car.

interface Car
{
String TOYOTA= “Toyota”;
String NISSAN= “Nissan”;
String HONDA = “Honda”;
void CarBrand();
}

class Toyota implements Car
{
private int engineCapacity;
private String condition;
private boolean sedan;

public Toyota(int engineCapacity, String Condition, boolean sedan)
{
this.engineCapacity = engineCapacity;
this.condition = condition;
this.sedan = sedan;
System.out.print(“Toyota im here”);
}

@Override
public void CarBrand()
{
System.out.println(“Toyota”);
}
}

class Nissan implements Car
{
private int engineCapacity;
private String condition;
private boolean sedan;

public Nissan(int engineCapacity, String Condition, boolean sedan)
{
this.engineCapacity = engineCapacity;
this.condition = condition;
this.sedan = sedan;
}

@Override
public void CarBrand()
{
System.out.println(“Nissan”);
}
}

class Honda implements Car
{
private int engineCapacity;
private String condition;
private boolean sedan;

public Honda(int engineCapacity, String Condition, boolean sedan)
{
this.engineCapacity = engineCapacity;
this.condition = condition;
this.sedan = sedan;
}

@Override
public void CarBrand()
{
System.out.println(“Honda”);
}
}

class CarSale
{
public static Car createCar(String brandName)
{
if(brandName == null || brandName.equals(“”))
return null;
if(brandName.equals(Car.TOYOTA))
return new Toyota(1500, “Brand New”, true);
else if(brandName.equals(Car.NISSAN))
return new Nissan(1300, “Registered”, true);
else if(brandName.equals(Car.HONDA))
return new Honda(1500, “Registered”, false);
else
return null;
}
}

public class FactoryDesign{
public static void main(String arg[])
{
Car car1 = CarSale.createCar(“Toyota”);
Car car2 = CarSale.createCar(“Nissan”);
Car car3 = CarSale.createCar(“Honda”);
}
}

Factory design pattern advantages

Factory design pattern allow us to write the code that can change easily change according to requirement. Because the object creation placed at one place that can handle easily. We code for interface rather than implementation.

Factory pattern promotes the loose-coupling, robustness and easy to extends. In future if we will get new requirement then we need to change in Factory class not in all the subclasses.

Factory pattern creates an abstraction layer between implementation and client classes and allows the sub-classes to choose the type of objects to create.

It works based on a Single Responsibility Principle. Because the object creation code can be move at one place that making the code easier to support.

Software Engineer @ WTS, Bachelor of Engineering (BEng) Honours in Software Engineering Graduated from University of Westminster