Singleton Explained in Simple Words
Ever heard the word Singleton while coding and felt it sounds fancy and complicated? Don't worry. By the end of this short blog, you'll fully get it.
Ever heard the word Singleton while coding and felt it sounds fancy and complicated? Don't worry. By the end of this short blog, you'll fully get it. Promise.
What is a Singleton?
A Singleton is a class that can only have one single object in your whole app. No matter how many times you try to create it, you always get the same one.
That's it. One class, one object, forever.
A Real-Life Example
Think about the President of a country. There is only one president at a time. If ten people in the country say "I want to talk to the president," they all talk to the same person. Nobody creates a brand-new president each time.
A Singleton works the same way. Everyone who asks for it gets the same object.
Other everyday examples:
- The TV remote in your house. One remote controls the TV. You don't make a new remote every time you change the channel.
- The printer in an office. One printer, and everyone sends their files to that same one.
Why Do We Even Need It?
Sometimes you want one shared thing that the whole app uses. For example:
- A database connection (you don't want to open 100 connections by accident)
- An app settings object (dark mode, language, etc. should be the same everywhere)
- A logger that writes all your logs to one place
If every part of your code made its own copy, things would get messy and waste memory. A Singleton keeps everything neat by sharing one copy.
How It Works (Simple Code)
Here's the idea in plain steps:
- Hide the normal way of creating the object (so people can't make new ones).
- Keep one secret copy inside the class.
- Give everyone a way to get that one copy.
Here it is in Dart:
class AppSettings {
// 1. The one and only copy, stored privately
static final AppSettings _instance = AppSettings._internal();
// 2. A private constructor so no one can do `AppSettings()`
AppSettings._internal();
// 3. Everyone gets the same copy through this
factory AppSettings() {
return _instance;
}
String theme = "light"; }
Now anywhere in your app:
```dart
var a = AppSettings();
var b = AppSettings();
a.theme = "dark";
print(b.theme); // prints "dark"
Notice that a and b are the same object. When you change a, b changes too, because they point to one shared thing.
The same idea in JavaScript:
class Logger {
constructor() {
if (Logger.instance) {
return Logger.instance; // give back the existing one
}
Logger.instance = this;
}
log(message) {
console.log("LOG:", message);
}
}
const a = new Logger();
const b = new Logger();
console.log(a === b); // true — same object
The Good and The Bad
Good things:
- Saves memory (only one object exists).
- Easy to share data across the whole app.
- One clear place to manage something important.
Things to be careful about:
- It acts like a global variable, and too many globals can make code harder to test and debug.
- If many parts of your code change the same Singleton, it can get confusing to track who changed what.
- In apps with multiple threads, you have to be careful that two threads don't create two copies at the same time.
So use it when you truly need one shared thing, not just everywhere out of habit.
Quick Summary
- A Singleton means one object for the whole app.
- Like one president, one TV remote, or one printer.
- Great for shared stuff like settings, logging, or a database connection.
- Use it wisely, because it behaves like a global.
That's the whole concept. Next time someone says "let's make this a Singleton," you'll just smile and nod. 😎