Factory Method and Singleton Class


✅ Factory Method Pattern

Definition

The Factory Method Pattern is a creational design pattern that provides an interface for creating objects, but allows subclasses or helper classes to decide which class to instantiate.


Intent

To encapsulate object creation and promote loose coupling between the client and concrete classes.


Structure

  • Product → Interface or abstract class
  • Concrete Products → Implementations of the product
  • Factory Method → Method responsible for object creation


Example

// Product
interface Shape {
    void draw();
}

// Concrete Products
class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

class Rectangle implements Shape {
    public void draw() {
        System.out.println("Drawing Rectangle");
    }
}

// Factory
class ShapeFactory {
    public static Shape getShape(String type) {
        if (type.equalsIgnoreCase("circle")) {
            return new Circle();
        } else if (type.equalsIgnoreCase("rectangle")) {
            return new Rectangle();
        }
        return null;
    }
}

// Client
public class Main {
    public static void main(String[] args) {
        Shape shape = ShapeFactory.getShape("circle");
        shape.draw();
    }
}
        

Advantages

  • Promotes loose coupling
  • Follows Open/Closed Principle
  • Centralizes object creation logic


Limitations

  • Can introduce additional classes
  • Slightly increases complexity


Use Cases

  • When object creation logic is complex
  • When the system should be independent of object instantiation
  • Framework-level design (e.g., internal object creation in libraries)


✅ Singleton Pattern

Definition

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it.


Intent

To control object creation and restrict instantiation to a single object.


Structure

  • Private constructor
  • Static instance variable
  • Public static access method


Example (Thread-Safe, Recommended)

class Singleton {

    private Singleton() {}

    private static class Holder {
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return Holder.INSTANCE;
    }
}
        

Characteristics

  • Lazy initialization
  • Thread-safe without synchronization overhead
  • Prevents multiple object creation


Advantages

  • Controlled access to a single instance
  • Saves memory
  • Useful for shared resources


Limitations

  • Can introduce global state
  • Harder to test (tight coupling)
  • May violate Single Responsibility Principle if misused


Use Cases

  • Logging systems
  • Configuration management
  • Database connections
  • Caching mechanisms


Conclusion

  • The Factory Method Pattern abstracts and delegates object creation.
  • The Singleton Pattern restricts instantiation to a single object.
  • Both are creational design patterns, but they solve fundamentally different problems:

To view or add a comment, sign in

Explore content categories