Singleton
ELI5 — The Vibe Check
Singleton is a pattern that ensures only ONE instance of a class exists in your entire app. It's like the CEO — there's only one, everyone talks to the same person, and creating a second CEO would cause chaos. Database connections and config objects are often singletons.
Real Talk
The Singleton pattern is a creational design pattern that restricts instantiation of a class to a single object and provides a global access point to it. Often criticized for introducing global state, making testing harder and creating hidden dependencies.
Show Me The Code
class Config {
private static instance: Config;
private constructor() {}
static getInstance(): Config {
if (!Config.instance) {
Config.instance = new Config();
}
return Config.instance;
}
}
When You'll Hear This
"The logger is a Singleton — same instance everywhere." / "Singletons are controversial because they make unit testing harder."
Related Terms
Anti-Pattern
Anti-Pattern is the opposite of a design pattern — it's a commonly used approach that looks like it solves a problem but actually makes things worse.
Dependency Injection
Instead of your UserService creating its own DatabaseConnection (tight coupling), you pass the database in from outside: new UserService(db).
Design Pattern
Design patterns are like recipe cards for solving common coding problems.