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:

Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 2024 â‹… 13 min read
Solving Eventual Consistency In Frontend

Solving eventual consistency in frontend

Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.

Kayode Adeniyi
Nov 19, 2024 â‹… 6 min read
How To Use Lazy Initialization Pattern With Rust 1.80

How to use the lazy initialization pattern with Rust 1.80

Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.

Yashodhan Joshi
Nov 18, 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