DEV Community

Cover image for The Singleton Design Pattern
Matheus Bernardes Spilari
Matheus Bernardes Spilari

Posted on

The Singleton Design Pattern

Have you ever been in a house with just one remote control for the TV? No matter who wants to change the channel or adjust the volume, everyone has to use that one single remote. That’s exactly the idea behind the Singleton Design Pattern.

Let’s explore this concept in the simplest way possible.

What is the Singleton Pattern?

The Singleton pattern makes sure that a class has only one instance, and it provides a global way to access it. Think of it like that single remote control in your living room — there's only one, and everyone shares it.


Real-Life Analogy: The Remote Control

Imagine you have a house with multiple family members. Instead of giving each person a different remote (which could get chaotic), you keep just one that everyone uses.

Why? Because having one remote:

  • Saves money (no need for 5 remotes),
  • Keeps things in sync (everyone controls the same device),
  • Avoids confusion.

This is exactly why we use the Singleton pattern in programming!


Java Code: The RemoteControl Singleton

Here’s how we can translate our single remote control into code:

public class RemoteControl {
    private static RemoteControl instance;

    // Private constructor – no one else can create it
    private RemoteControl() {
        System.out.println("Remote is ready to use.");
    }

    // Public method to get the only instance
    public static RemoteControl getInstance() {
        if (instance == null) {
            instance = new RemoteControl();
        }
        return instance;
    }

    public void pressButton(String button) {
        System.out.println("Button pressed: " + button);
    }
}
Enter fullscreen mode Exit fullscreen mode

How to Use It

Now let’s see how this works in action:

public class LivingRoom {
    public static void main(String[] args) {
        RemoteControl myRemote = RemoteControl.getInstance();
        myRemote.pressButton("Power");

        RemoteControl momRemote = RemoteControl.getInstance();
        momRemote.pressButton("Volume Up");

        System.out.println(myRemote == momRemote);  // true – same remote!
    }
}
Enter fullscreen mode Exit fullscreen mode

Even though two people (you and your mom) try to get a remote, they both get the same one. That’s the Singleton pattern at work.


Why Use Singleton?

  • Keeps things consistent
  • Saves resources, reduced memory footprint, especially when the object is heavy to instantiate.
  • Global access point, making it easy to access from anywhere in your code.

When to Be Careful

  • Singleton is like global state — if overused, it can make code harder to test and maintain
  • Not great if you need multiple versions of something (e.g., two different remotes for two TVs)
  • Multithreading issues, if not implemented carefully.
  • Can be abused, used where simpler solutions would suffice (like dependency injection).

Conclusion

Singleton isn’t just a fancy design pattern — it’s a practical way to manage shared things in your app, like settings, logging, or yes… your one-and-only TV remote.


📍 Reference

💻 Project Repository

👋 Talk to me

Top comments (0)