Difference Between Singleton and Factory Design Pattern in Java Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like 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 JavaThe 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 JavaThe 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 JavaCharacteristics 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 Comment More infoAdvertise with us Next Article Difference Between Singleton and Factory Design Pattern in Java M maha123 Follow Improve Article Tags : Java Difference Between Geeks Premier League Geeks Premier League 2023 Practice Tags : Java Similar Reads Difference Between Singleton Pattern and Static Class in Java Singleton Pattern belongs to Creational type pattern. As the name suggests, the creational design type deals with object creation mechanisms. Basically, to simplify this, creational pattern explains to us the creation of objects in a manner suitable to a given situation. Singleton design pattern is 6 min read Spring - Difference Between Dependency Injection and Factory Pattern Dependency Injection and Factory Pattern are almost similar in the sense that they both follow the interface-driven programming approach and create the instance of classes. A. Factory Pattern In Factory Pattern, the client class is still responsible for getting the instance of products by class getI 4 min read Difference Between Constructor and Static Factory Method in Java Whenever we are creating an object some piece of code will be executed to perform initialization of that object. This piece of code is nothing but a constructor, hence the main purpose of the constructor is to perform initialization of an object but not to create an object. Let us go through the bas 4 min read Singleton Design Pattern in Java Singleton Design Pattern is a creational design pattern that ensures a class has only one instance and provides a global point of access to it. This pattern is particularly useful when exactly one object is needed to coordinate actions across the system. Important Topics for Singleton Method in Java 5 min read Difference Between Static and Non Static Nested Class in Java Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin 4 min read Like