Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing the Singleton Pattern in Java

Tech May 9 3

The Singleton pattern guarantees that a class has only one unique instance and supplies a unified global entry point for accessing that instance. It is particularly useful when you need to strict control the number of object instances to save system resources or maintain consistent state.

A valid Singleton implementation must meet three core requirements:

  1. The class restricts itself to a single instance.
  2. The class takes full resopnsibility for creating that instance.
  3. The class provides a public method for the rest of the system to retrieve the instance.

This is typically achieved by making the constructor private to prevent external instantiation and exposing a static method to return the pre-created instance.

Consider a country that can only have one active leader at a time. The following code demonstrates an eager initialization approach:

package com.example.design;

public class NationalLeader {
    // Eagerly initialize the instance when the class loads
    private static final NationalLeader instance = new NationalLeader();

    // Private constructor prevents external instantiation
    private NationalLeader() {}

    // Global access point
    public static NationalLeader getLeader() {
        return instance;
    }

    public void addressCitizens() {
        System.out.println("Greetings to all citizens!");
    }
}

class Application {
    public static void main(String[] args) {
        NationalLeader leader1 = NationalLeader.getLeader();
        leader1.addressCitizens();

        NationalLeader leader2 = NationalLeader.getLeader();
        leader2.addressCitizens();

        System.out.println("Identity check (same object?): " + (leader1 == leader2));
    }
}

Output:

Greetings to all citizens!
Greetings to all citizens!
Identity check (same object?): true

A classic example of this pattern in the Java Development Kit is the Runtime class, which allows accesss to the environment in which the application is running:

public class Runtime {
    private static Runtime currentRuntime = new Runtime();

    public static Runtime getRuntime() {
        return currentRuntime;
    }

    private Runtime() {}
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.