Amy Blankenship
Sep 14, 2024

--

It's all fun and games until you need to have more than one and only one is enforced everywhere in your architecture. If it's provided by DI, it's by definition not a Singleton. You could inject this one in this place and that one in that place.

The Singleton pattern is enforced by calling exactly one getInstance function and that getInstance function is part of the Class for the instance--so you can't replace it by calling a different getInstance on a different Class. The specific Class is compiled into every place that calls getInstance.

Choosing to instantiate only one instance of a thing (small s singleton) and inject that is not Singleton pattern and is actually very good architecture.

--

--

Amy Blankenship
Amy Blankenship

Written by Amy Blankenship

Full Stack developer at fintech company. I mainly write about React, Javascript, Typescript, and testing.

Responses (1)