Skip to main content
22 votes
Accepted

Modern C++ Singleton Template

Okay so first of the obligatory Singletons are bad practice so you probably shouldn't make it easy to write bad code. Ignoring the fact that the class probably shouldn't exist at all we can look at ...
Emily L.'s user avatar
  • 16.7k
19 votes
Accepted

Singleton Design Pattern implementation in a not traditional way

Your implementation does the trick. For what it's worth, I would consider your implementation the current "traditional way". It is thread-safe. The static constructor is guaranteed to run only once, ...
JAD's user avatar
  • 2,959
15 votes

Modern C++ Singleton Template

Singletons make it hard to test your code, and in my job I'd reject this at review for encouraging the development of untestable features. That said, I'll continue reviewing despite that. No need for ...
Toby Speight's user avatar
  • 88.3k
9 votes

Singleton class and correct way to access it in C++

Thread safety Accessing the singleton is thread-safe. The change in C++11 has forced compilers to implement the construction of local static variables (like ...
hoffmale's user avatar
  • 6,528
9 votes

Singleton Design Pattern implementation in a not traditional way

There's a potential bug with this scenario. According to this Jon Skeet post, if an exception is thrown inside the static constructor, it is never retried. Which ...
IEatBagels's user avatar
  • 12.7k
6 votes

Two-way data binding

A few notes: This data binding is not two way. If it was two way I should be able to update the either the view or the model and have changes propagate to the other. See this SO post. The code you ...
Gerrit0's user avatar
  • 3,501
6 votes
Accepted

Storing singleton instances in a generic utils class

Let me just express the first thing that came to mind when I read this question's title: Not another Singleton question. Let's ignore in the following that Singletons are an anti-pattern in Object-...
Vogel612's user avatar
  • 25.5k
6 votes
Accepted

ASP.NET Core singleton with a thread-safe property that can be changed

That will lock it down for sure. If you know you will have more reads then writes, which is common, you should look at the ReaderWriterLockSlim class. What you have will only allow one read and one ...
CharlesNRice's user avatar
  • 4,438
5 votes
Accepted

Singleton metaclass for each argument set compatible with Pickle

The point of having to define either neither or both of __init__ and __reduce_ex__ is to set ...
Peilonrayz's user avatar
  • 44.6k
5 votes

Singleton in Ruby without using Singleton module or class variable

The Singleton module in Ruby for instance also guarantees synchronised access, e.g. if multiple threads try to create an instance your implementation would not work. https://github.com/ruby/ruby/blob/...
Christian Bruckmayer's user avatar
4 votes
Accepted

HttpClient wrapper for simple calls with optional cert

As is this does not work because multiple requests will concurrently operate on the same variables (instance variables and HttpClient internal state as it is being configured). This is fairly easy to ...
usr's user avatar
  • 745
4 votes
Accepted

Implementation of an OkHttp singleton for Android

You don't need to create new instance of OkHttpClient. Remove it. You can use Initialization-on-demand holder pattern to create singleton in Java. No need to use <...
thangdc94's user avatar
  • 216
4 votes
Accepted

Several approaches to a logging class

Singletons There are several ways to make something a singleton. You are doing it by removing the public constructor, and forcing users to get an instance through a static member function. That works,...
G. Sliepen's user avatar
  • 69.3k
4 votes
Accepted

Multilingual app without localizing

I did something like this. You can also just use a Strings file. What I did was create a spreadsheet in the Google Drive with all the translations (one column for the identifier BUTTON_TITLE_OK and ...
Joris Weimar's user avatar
4 votes
Accepted

Simple Publisher Subscriber in Java

Initializing object variables ...
mdfst13's user avatar
  • 22.4k
4 votes
Accepted

C++ Settings Manager

A better way to make a singleton You don't need to use a std::shared_ptr to make a singleton, and you also can do it with just one class, so no need for a "...
G. Sliepen's user avatar
  • 69.3k
3 votes

Create another Singleton object from a Singleton class

It has been some time since I last wrote Java, but I have some general suggestions. Before focusing on specific details of the code I would recommend you to spend some time on indentation. Having ...
AnotherGuy's user avatar
  • 1,104
3 votes

Singleton rewrite based on old one

Toby Speight's answer is correct for a normal singleton, but the class that you're replacing doesn't look like a normal singleton. The original class appears to allow the user to explicitly define ...
user673679's user avatar
  • 12.2k
3 votes
Accepted

Multi-threaded Time Scheduler without polling

You leak memory in various places, particularly the thread objects you create. They're allocated with new, and joined, but ...
1201ProgramAlarm's user avatar
3 votes

Java MongoDB client as a singleton

Simple answer: No. Unless if your application only ever runs on 1 thread. The easiest way to lazy initialise a singleton thread safe would be with a holder pattern. Example from wikipedia: ...
Imus's user avatar
  • 4,387
3 votes

ObjectManager - Single Instance Object managerment

1) What's the point of storing sprites on server-side? Sounds counterintuitive. Normally all such assets are located on the client. Sharing sprites over the network usually requires too much bandwidth....
Nikita B's user avatar
  • 13.1k
3 votes

Singleton class to contain a DocumentClient instance, that can be overridden for testing

This answer only has one tiny section (IDisposable) that I feel is an appropriate kind of answer for Code Review. The rest is more suitable as a StackOverflow ...
Svek's user avatar
  • 1,023
3 votes
Accepted

Destroy Singleton pattern in kotlin

If you want a thread safe singleton in Kotlin, you should re-write your singleton like so: ...
Thomas Cook's user avatar
3 votes

Fast efficient C++ Singleton template with proper constructor and destruction order

style nit: inline static singletons_meta_data singleton_meta_data_node{nullptr, nullptr, nullptr, nullptr, 0, 0}; Specifying all the member values like this ...
butt's user avatar
  • 844
3 votes
Accepted

Alpha finance trader singleton

You're not using the singleton pattern since there is no logic that prevents you from constructing 2 objects of that type. Creating and using only one object is completely fine. Notable examples are: <...
QuasiStellar's user avatar
  • 2,327
2 votes

Modern C++ Singleton Template

When working with static and shared libraries, one must be careful that you don't have several implementations of the instance() function. That would lead to hard ...
Paul Groskopf's user avatar
2 votes

Threadsafe DBContext in singleton

I'm thinking of a few solutions: one would be is not making this a singleton at all, but instantiate a logger each time I need one. I can think of added overhead as a disadvantage for this ...
Shadetheartist's user avatar
2 votes

ObjectManager - Single Instance Object managerment

Witout questioning your design here are a couple of improvements you can make. var obj = new T(); In your case creating this dummy and not actually used objects ...
t3chb0t's user avatar
  • 44.7k
2 votes

Static class for constants with reference to java resources

well as pointed out using singletons is considered bad practice and using global constants in an object-orientatd world is bad practice as well... So, what is your suggestion? Consider the purpose ...
Martin Frank's user avatar
  • 3,033
2 votes

Several approaches to a logging class

You chose a good implementation but then made something weird Your Singleton implementation is a good one -it is the canonical implementation in C++ anyway, its ...
papagaga's user avatar
  • 5,817

Only top scored, non community-wiki answers of a minimum length are eligible