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:

Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 â‹… 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 â‹… 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 â‹… 7 min read
Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 â‹… 10 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