2021-02-11
2568
#vanilla javascript
Ashley Davis
34185
Feb 11, 2021 â‹… 9 min read

You’re wrong about singletons

Ashley Davis Ashley Davis is a software craftsman and author. He is VP of Engineering at Hone and currently writing Rapid Fullstack Development and the second edition of Bootstrapping Microservices. Follow on Twitter for updates.

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 min read
View all posts

5 Replies to "You’re wrong about singletons"

  1. The only acceptable use case for singletons is if you have no access to the entrypoint / initialization phase of your application.

    I don’t think that using singletons to pass values into your DI container is “using” singletons. You’re just working around the fact that you cannot access the entrypoint of your application to pass a single instance of your class into the DI container.

    The backlash against singletons comes from the tight coupling you’re introducing by making the singleton instance the only instance that every class can access. I would argue that this is more of a promotion for DI rather than a promotion for singletons.

    1. Yep, totally agree although I do have access to the entry point, so not really sure what you mean by that.

      Yes! This is a promotion for DI. It’s so awesome you could see that.

  2. Congratulations, you just made the exact same argument as all other articles explaining what to do _instead of_ the singleton pattern, except while trying to sound like you’re defending the pattern.

    Single instance != singleton
    Single global instance != singleton
    singleton == is internally _enforced_ to only ever have once instance

    1. Thanks so much. You are correct of course, if you wanted to use the singleton pattern this is what makes it usable! You obviously know that already so maybe this article isn’t for you.

Leave a Reply