CPPCON2020 Session Review: Retiring the Singleton Pattern: Concrete suggestions for what to use instead

The Singleton design pattern has been discussed ad naseaum over the years. Peter Muldoon starts with the motivation for this talk: an occurrence of Singleton in the workplace, oh my!

The premise of the talk, is not whether to keep it, but what to replace it with.

Over the years, I’ve seen people being captivated by Singleton, as some sort of the Highlander syndrome, where “there can only be one”. In Singling Out Singleton, I have a short blog stating that Singleton refers to single state, rather than a single instance, and even provided an option–what I call the Nifty Singleton, inspired by John Lako’s technique, which in itself, if I remember correctly, came about in the C++ ARM book of old. For a modern alternative, one can use std::call_once and std::once_flag, which I don’t consider a singleton, but rather a requirement of the particular problem at hand. You can also resort to libraries such as Boost.

In my particular case, I’ve lived very well without it, thank you very much. Perhaps Singleton should be relegated to the annals of Design Patterns lore and used only pedagogically?

Peter takes a more wholesome approach, tackling different aspects that arise with a replacement. You can “cut to the chase” and go to minute 59 of the talk if you want to, but than would have to back trace to understand the reasons. Some of the problems mentioned go beyond Singleton, and we can all learn from it.

This entry was posted in C++, C++17, C++20, design patterns and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s