Factory
Definition
Class Diagram
Example: Shape
Implementation
Simple Factory
Factory Method
Factory Method vs Simple Factory
Resources
Video → 27. All Creational Design Patterns | Prototype, Singleton, Fac
tory, AbstractFactory, Builder Pattern
Video → 5. Factory Pattern Vs Abstract Factory Pattern Explanation
(Hindi) | Low Level System Design
Definition
The Factory Method Pattern is used when we want to encapsulate object creation,
instantiation, and all related business logic in one place.
This approach offers an interface for creating objects without needing to specify their exact classes. It delegates
the responsibility of object creation to subclasses, which implement specific functionalities. Instead of using the
"new" keyword directly to create objects, we utilize a factory method that returns objects adhering to a
common interface. The actual class that gets instantiated is determined at runtime. This pattern encourages loose
coupling and enhances extensibility.
Class Diagram
Example: Shape
1. Product (Shape)
Defines the interface for objects that the factory method creates
All concrete shapes implement this interface
2. Concrete Products (Circle, Square, Rectangle, Triangle)
Specific implementations of the Shape interface
Each has its own drawing logic and area calculation
3. Creator (ShapeFactory)
Abstract class that declares the factory method
May provide default implementation or template methods
4. Concrete Creators (CircleCreator, RectangleCreator, etc.)
Override the factory method to return specific product instances
Each factory knows how to create one type of shape
Implementation
1 // Step 1: Define the Product interface
2 public interface Shape {
3 void computeArea();
4 void draw();
5 }
6
7 // Step 2: Concrete Product classes
8 public class Circle implements Shape {
9
10 @Override
11 public void computeArea() {
12 [Link]("Inside Circle::computeArea() method.");
13 }
14
15 @Override
16 public void draw() {
17 [Link]("Inside Circle::draw() method.");
18 }
19 }
20 public class Rectangle implements Shape {
21
22 @Override
23 public void computeArea() {
24 [Link]("Inside Rectangle::computeArea() method.");
25 }
26
27 @Override
28 public void draw() {
29 [Link]("Inside Rectangle::draw() method.");
30 }
31 }
32 public class Square implements Shape {
33
34 @Override
35 public void computeArea() {
36 [Link]("Inside Square::computeArea() method.");
37 }
38
39 @Override
40 public void draw() {
41 [Link]("Inside Square::draw() method.");
42 }
43 }
Simple Factory
1 public enum ShapeType {
2 CIRCLE, RECTANGLE, SQUARE
3 }
4 public class ShapeFactory {
5 public static Shape createShapeInstance(ShapeType shapeType) {
6 if (shapeType == null) {
7 return null;
8 }
9 return switch (shapeType) {
10 case CIRCLE -> new Circle();
11 case RECTANGLE -> new Rectangle();
12 case SQUARE -> new Square();
13 default -> throw new IllegalStateException("ShapeType
doesn't exist!");
14 };
15 }
16 }
1 // Step 3: Simple Factory Demo (Bloated Design)
2 public class SimpleFactoryDemo {
3 public static void main(String[] args) {
4 [Link]("======= Simple Factory Design Pattern
======");
5
6 // set the type you want
7 ShapeType shapeType = [Link];
8 // get the shape
9 Shape shape = [Link](shapeType);
10 [Link]();
11 [Link]();
12 }
13 }
Factory Method
1 // Step 3: Abstract Creator class
2 public abstract class ShapeFactory {
3 // Factory method - to be implemented by subclasses
4 public abstract Shape createShape();
5 }
6
7 // Step 4: Concrete Creator classes
8 public class CircleCreator extends ShapeFactory {
9
10 @Override
11 public Shape getShapeInstance() {
12 return new Circle();
13 }
14 }
15 public class RectangleCreator extends ShapeFactory {
16
17 @Override
18 public Shape getShapeInstance() {
19 return new Rectangle();
20 }
21 }
22 public class SquareCreator extends ShapeFactory {
23 @Override
24 public Shape getShapeInstance() {
25 return new Square();
26 }
27 }
1 // Step 5: Client code demonstration
2 public class FactoryMethodDemo {
3
4 public static void main(String[] args) {
5 [Link]("======= Factory Method Design Pattern
======");
6 // set the type you want
7 ShapeType shapeType = [Link];
8 // get the shape
9 Shape shape = getShapeInstance(shapeType);
10 // perform operations
11 [Link]();
12 [Link]();
13 }
14
15 private static Shape getShapeInstance(ShapeType shapeType) {
16 Shape shape = null;
17 if (shapeType == null) {
18 return null;
19 }
20 switch (shapeType) {
21 case CIRCLE:
22 ShapeFactory circleCreator = new CircleCreator();
23 shape = [Link]();
24 break;
25 case RECTANGLE:
26 ShapeFactory rectangleCreator = new
RectangleCreator();
27 shape = [Link]();
28 break;
29 case SQUARE:
30 ShapeFactory squareCreator = new SquareCreator();
31 shape = [Link]();
32 break;
33 default:
34 throw new IllegalStateException("ShapeType doesn't
exist.");
35 }
36 return shape;
37 }
38 }
Factory Method vs Simple Factory
Simple Factory (shown in the example):
Static method that creates objects based on parameters
Not a true design pattern, more of a programming idiom
Violates the Open/Closed principle when adding new types
Factory Method Pattern (shown in the example):
Uses inheritance and polymorphism
Each concrete creator handles one product type
Follows the Open/Closed principle perfectly
The Factory Method Pattern is especially useful in frameworks and libraries that require extension points for
users to customize object creation behavior.