Factory Design Pattern in Java
Type: Creational Design Pattern
Summary
Factory or Factory method Design pattern is a creational design pattern. It helps in loose coupling between client and the actual object that the Client needs. Rather than Client calling the constructor using new operator it calls the Factory and the factory returns the respective object.
Details
Factory method design pattern encapsulate the complexities around creating an object and also uses Inheritance by letting factory method to decide which Subclass instance is needed to be returned.
Factory method pattern solves the issue of creating object without exposing internal details by
Provide an interface (Could be an Interface or Abstract Class) and a factory method
let the Client create the appropriate object by calling the factory method.
Example
Let's say we have a company ABC Cars Corp, which manufactures different type's of Cars (Sedan, SUV etc). Now we want to provide a way to the Sales man to be able to get a Car (object instance) based on Customer preference, and can demo those cars to Customers.
One option is to provide constructor for those Cars. So that Client can call the constructors and create cars.
That exposes a lot of technical details to the Sales Man. Better would be have a factory/or room which has all available cars. Then the Sales Man can could quickly turn around with available Car to the Customer. That way the learning curve for the Sales Man is very minimum.
Now say down the lane, ABC Cars Corp also starting manufacturing Sports Car. Now the problem is, all the Sales Team needs to know what parameters needed to be sent to create a sample Sports cars.
Factory method does that by utilizing inheritance and based on the requirement it creates a specific instance of one of the SubClass.
GasolineCarFactory is the Factory that implements a factory method.
Car is extended by two Subclasses SUVCar and CompactSedan Car
Car is extended by two Subclasses SUVCar and CompactSedan Car
Class Diagram
Sample Code
// subclass of Car
public class CompactSedanCar extends Car {
@Override
public String getModel() {
return "Compact4person";
}
@Override
public String getPrice() {
return "20k";
}
@Override
public String getType() {
return "COMPACT";
}
}
// subclass of Car
public class SUVCar extends Car {
@Override
public String getModel() {
return "SUV2019";
}
@Override
public String getPrice() {
return "40k";
}
@Override
public String getType() {
return "SUV";
}
}
// factory method Class which the Client interact with
public class GasolineCarFactory {
public Car getCarType(int i) {
switch (i) {
case 1:
return new CompactSedanCar();
case 2:
return new SUVCar();
default:
return new CompactSedanCar();
}
}
}
Github Code
Advantages
Centralized interface to create object.
Easy of refactoring and code reuse.
Easy discovery for developers.
Better testable codes.
Internally the factory method can utilize Prototype design pattern or Flyweight design pattern as per the situation.
Easy of refactoring and code reuse.
Easy discovery for developers.
Better testable codes.
Internally the factory method can utilize Prototype design pattern or Flyweight design pattern as per the situation.
Disadvantages
Factory method design pattern adds a layer of attraction between Client and real Object.
when a new SubClass is introduced the factory method needs to update to take that into consideration.
When to Use
When we want to provide a centralized or common interface to create multiple objects (subclasses)
When we want to hide the complexities of creating instances of those object.
When we want to control how an object instance should be created.
If we have complex business logic while creating objects, this design pattern is very useful
When we want to control how an object instance should be created.
If we have complex business logic while creating objects, this design pattern is very useful
Comments
Post a Comment