Difference Between Singleton and Factory Design Pattern in Java
Last Updated :
27 Nov, 2023
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 Singleton instance = null ;
private Singleton() {}
public static Singleton getInstance()
{
if (instance == null ) {
instance = new Singleton();
}
return instance;
}
public void showMessage()
{
System.out.println(
"Hello, I am the Singleton instance." );
}
public static void main(String[] args)
{
Singleton singleton = Singleton.getInstance();
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 {
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)
{
ShapeFactory shapeFactory = new ShapeFactory();
Shape shape1 = shapeFactory.getShape( "rectangle" );
shape1.draw();
Shape shape2 = shapeFactory.getShape( "circle" );
shape2.draw();
}
}
|
Output:
Drawing a rectangle.
Drawing a circle.
Difference Between Singleton and Factory Design Pattern in Java
Ensures a single instance of class and provides global access
|
Creates the objects without exposing the creation logic
|
Only one instance per class
|
The Multiple instances can be created as needed
|
The singleton class controls its own instantiation using the method like getInstance()
|
A factory class or method is responsible for the object creation
|
Limited to single instance
|
Can create multiple instances with the different configurations
|
java.lang.Runtime Spring’s ApplicationContext
|
java.util.Calendar javax.xml.parsers.DocumentBuilderFactory
|
When you need exactly one instance to manage the shared resources or settings
|
When you want to centralize object creation and decouple code
|
Share your thoughts in the comments
Please Login to comment...