Creational Design Pattern
![](https://codelido.com/assets/files/2022-12-17/1671307935-285107-single-ton-design-pattern-2.jpg)
Definition
The factory design pattern is used when we have a superclass with multiple subclasses and based on input, we need to return one of the subclasses. This pattern takes out the responsibility of the instantiation of a Class from the client program to the factory class. We can apply a singleton pattern on the factory class or make the factory method static.
Characteristics
- It provides a way to encapsulate object creation and reuse it.
- It separates the instantiation of a class from its usage.
- It promotes loose coupling between objects and their usage.
- It allows for the creation of multiple types of objects from a single interface.
- It can be used to create objects that are not known at compile time.
- It allows for the reuse of existing code.
- It provides a way to create objects of a certain type without knowing the exact implementation details.
UML Diagram
![](https://codelido.com/assets/files/2022-12-17/1671308008-320334-image.png)
Code
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
public class ShapeFactory {
//use getShape method to get object of type shape
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
}
return null;
}
}
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
//get an object of Circle and call its draw method.
Shape shape1 = shapeFactory.getShape("CIRCLE");
//call draw method of Circle
shape1.draw();
//get an object of Rectangle and call its draw method.
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//call draw method of Rectangle
shape2.draw();
//get an object of Square and call its draw method.
Shape shape3 = shapeFactory.getShape("SQUARE");
//call draw method of square
shape3.draw();
}
}
Output
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Advantages
- Factory design pattern provides loose coupling between classes.
- It simplifies the object creation process and makes the code more maintainable.
- Factory pattern allows the code to be independent of how the objects are created, composed, and represented.
- It allows for the addition of new types of objects without having to modify existing code.
Disadvantages
- Factory design pattern can be difficult to implement in some cases.
- It can become complex as the number of different objects to be created increases.
- It can be difficult to maintain if the object creation process changes often.