Factory design pattern

Factory design pattern

The Factory Design Pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. Rather than creating objects directly, the factory method defines an interface for creating an object, but lets subclasses decide which class to instantiate. This pattern is also known as the  Virtual Constructor.

Factory design pattern

Advantages of Factory Design Pattern

1. Encapsulation

   The Factory pattern encapsulates the object creation process, which makes the code more modular and easier to manage.

2. Loose Coupling

  It promotes loose coupling by reducing the dependency between client code and the classes it needs to instantiate, allowing for more flexibility and easier maintenance.

3. Flexibility

   Factories can return different classes or objects based on input parameters or internal logic, which allows for more flexibility in the code.

4. Enhanced Readability and Maintainability

   By using factories, the code becomes more readable and maintainable since the instantiation logic is centralized in one place.

Disadvantages of Factory Design Pattern

1. Complexity

   The Factory pattern can introduce additional complexity into the codebase, especially if overused or applied to simple scenarios where it is not necessary.

2. Overhead:

   Factories can introduce a slight overhead, particularly when dealing with simple object creation that doesn’t require a factory.

3. Inflexibility in Simple Applications

  In simpler applications, the use of a factory can be overkill and make the code less intuitive to read and understand.

Example - Factory Design Pattern:

Here's a practical example of the Factory Design Pattern in Java:

java
// Step 1: Create an interface
interface Shape {
    void draw();
}

// Step 2: Create concrete classes implementing the same interface
class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

// Step 3: Create a Factory to generate object of concrete class based on given information
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;
    }
}

// Step 4: Use the Factory to get the object of concrete class by passing an information such as type
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");
        shape1.draw();

        // Get an object of Rectangle and call its draw method
        Shape shape2 = shapeFactory.getShape("RECTANGLE");
        shape2.draw();

        // Get an object of Square and call its draw method
        Shape shape3 = shapeFactory.getShape("SQUARE");
        shape3.draw();
    }
}

Explanation

  • Step 1: Define a Shape interface with a draw() method.
  • Step 2: Implement the Shape interface in Circle, Rectangle, and Square classes.
  • Step 3: Create a ShapeFactory class with a getShape(String shapeType) method that returns an instance of the Shape based on the provided type.
  • Step 4: In the FactoryPatternDemo class, use the ShapeFactory to get instances of Circle, Rectangle, and Square and call their draw() methods.

The Factory Design Pattern helps in encapsulating the object creation process, making the code more modular, flexible, and easier to maintain, despite some potential increase in complexity and overhead in simple scenarios.

Homepage

Readmore