Singleton Design Pattern — 3 Minute Series

One instance globally.

Elric Edward
Sep 17, 2022
Photo by Chetan Hireholi on Unsplash

_00 / Concept

In an application, the Singleton design pattern can ensure the app has only one instance globally.

_01 / Key Roles

class LogService {
private static instance = null;
private constructor() {}
static init() {
if (this.instance === null) {
this.instance = new LogService();
}
return this.instance
}
...
}
const logger = LogService.init();
logger...

_02 / Trade-offs

🟢 Eliminated unnecessary instantiation.
🔴 Introduce a global variable and states which is bad for testing.
🔴 Singleton solves less than it causes, so make sure you need it.

--

--