Open In App
Related Articles

Difference Between Singleton and Factory Design Pattern in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Design patterns are essential tools for creating maintainable and scalable software. Two commonly used design patterns in Java are the Singleton and Factory patterns. This article provides an introduction to the Singleton and Factory design patterns with examples.

Singleton Design Pattern in Java

The Singleton pattern is a creational design pattern that ensures that only one instance of a class can be created and that the same instance is used every time the object is requested.

Example:

Java

import java.io.*;
  
public class Singleton {
    // Private static variable to hold the single instance
    // of the class
    private static Singleton instance = null;
  
    // Private constructor to prevent outside instantiation
    private Singleton() {}
  
    // Public static method to get the single instance of
    // the class
    public static Singleton getInstance()
    {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
  
    // Public method to demonstrate the Singleton instance
    public void showMessage()
    {
        System.out.println(
            "Hello, I am the Singleton instance.");
    }
  
    public static void main(String[] args)
    {
        // Get the Singleton instance
        Singleton singleton = Singleton.getInstance();
  
        // Call a method on the Singleton instance
        singleton.showMessage();
    }
}

                    

Output:

Hello, I am the Singleton instance.

Factory Design Pattern in Java

The Factory pattern is a creational pattern that provides a way to create objects without specifying the exact class of object that will be created. It provides a factory method that is responsible for creating objects of the appropriate type based on a set of criteria, such as a type or a configuration parameter.

Example:

Java

import java.io.*;
  
interface Shape {
    void draw();
}
  
class Rectangle implements Shape {
    public void draw()
    {
        System.out.println("Drawing a rectangle.");
    }
}
  
class Circle implements Shape {
    public void draw()
    {
        System.out.println("Drawing a circle.");
    }
}
  
class ShapeFactory {
    // Factory method to create Shape objects based on the
    // input String
    public Shape getShape(String shapeType)
    {
        if (shapeType == null) {
            return null;
        }
        else if (shapeType.equalsIgnoreCase("rectangle")) {
            return new Rectangle();
        }
        else if (shapeType.equalsIgnoreCase("circle")) {
            return new Circle();
        }
        return null;
    }
}
  
public class FactoryPatternExample {
    public static void main(String[] args)
    {
        // Create a ShapeFactory object
        ShapeFactory shapeFactory = new ShapeFactory();
  
        // Get a Rectangle object and call its draw method
        Shape shape1 = shapeFactory.getShape("rectangle");
        shape1.draw();
  
        // Get a Circle object and call its draw method
        Shape shape2 = shapeFactory.getShape("circle");
        shape2.draw();
    }
}

                    

Output:

Drawing a rectangle.
Drawing a circle.

Difference Between Singleton and Factory Design Pattern in Java

Characteristics

Singleton Design Pattern

Factory Design Pattern

Purpose

Ensures a single instance of class and provides global access

Creates the objects without exposing the creation logic

Number of Instances

Only one instance per class

The Multiple instances can be created as needed

Construction

The singleton class controls its own instantiation using the method like getInstance()

A factory class or method is responsible for the object creation

Object Creation

Limited to single instance

Can create multiple instances with the different configurations

Example

java.lang.Runtime Spring’s ApplicationContext

java.util.Calendar javax.xml.parsers.DocumentBuilderFactory

Usage Scenario

When you need exactly one instance to manage the shared resources or settings

When you want to centralize object creation and decouple code



Last Updated : 27 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads