codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

6 / 100 topics
5Introduction to Creational Patterns6Singleton Pattern7Factory Method Pattern8Abstract Factory Pattern9Builder Pattern10Prototype Pattern31Practical Exercises for Creational Patterns
Tutorials/Design Patterns/Singleton Pattern
🎭Design Patterns

Singleton Pattern

Updated 2026-05-15
10 min read

Singleton Pattern

Introduction

In software design, patterns are reusable solutions to common problems. The Singleton pattern is one of the most well-known and widely used design patterns. It ensures that 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.

Concept

The core idea behind the Singleton pattern is to restrict the instantiation of a class to one "single" instance. This is typically achieved by making the constructor private or protected, and providing a static method that returns the instance of the class. If the instance does not exist, it creates one; otherwise, it returns the existing instance.

Key Characteristics

  1. Single Instance: Ensures that only one instance of the class exists.
  2. Global Access Point: Provides a global point of access to the instance.
  3. Lazy Initialization: The instance is created when it is needed for the first time (lazy initialization).

Examples

Let's explore how to implement the Singleton pattern in different programming languages.

Example 1: Basic Singleton Pattern in Java

In Java, you can implement the Singleton pattern by making the constructor private and providing a static method to access the instance.

Java
1public class Singleton {
2 // Static variable that holds the single instance
3 private static Singleton instance;
4
5 // Private constructor to restrict instantiation
6 private Singleton() {}
7
8 // Public static method to provide global access point
9 public static Singleton getInstance() {
10 if (instance == null) {
11 instance = new Singleton();
12 }
13 return instance;
14 }
15
16 // Example method to demonstrate functionality
17 public void showMessage() {
18 System.out.println("Hello from Singleton!");
19 }
20}
Terminal
$ javac Singleton.java
$ java Singleton
Output
Hello from Singleton!

Example 2: Thread-Safe Singleton in Java

To ensure thread safety, you can synchronize the getInstance() method.

Java
1public class ThreadSafeSingleton {
2 private static volatile ThreadSafeSingleton instance;
3
4 private ThreadSafeSingleton() {}
5
6 public static synchronized ThreadSafeSingleton getInstance() {
7 if (instance == null) {
8 instance = new ThreadSafeSingleton();
9 }
10 return instance;
11 }
12
13 public void showMessage() {
14 System.out.println("Hello from Thread-Safe Singleton!");
15 }
16}

Example 3: Singleton Pattern in Python

In Python, you can use the __new__ method to control object creation.

Python
1class Singleton:
2 _instance = None
3
4 def __new__(cls):
5 if cls._instance is None:
6 cls._instance = super(Singleton, cls).__new__(cls)
7 return cls._instance
8
9 def showMessage(self):
10 print("Hello from Singleton!")
Terminal
$ python singleton.py
Output
Hello from Singleton!

What's Next?

After mastering the Singleton pattern, you can explore other creational patterns such as the Factory Method Pattern. This pattern provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

By understanding and implementing design patterns like Singleton, you'll be better equipped to write robust, maintainable, and scalable code.


PreviousIntroduction to Creational PatternsNext Factory Method Pattern

Recommended Gear

Introduction to Creational PatternsFactory Method Pattern