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.6k
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,919
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
  • 76.8k
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,498
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.4k
6 votes

Thread safe templatized singleton class

Don't use the Singleton pattern But do you think it is really thread safe good singleton? Please excuse my pedantry and let me say that the only good use of the Singleton pattern is to not use it ...
5gon12eder's user avatar
  • 4,280
6 votes

Singleton method for Redis connection in Python

The Singleton pattern is a way to work around the absence of global variables in Java. But Python has global variables, and it's usually possible to write shorter and simpler initialization code if ...
Gareth Rees's user avatar
  • 49.6k
6 votes
Accepted

Client to connect to server and show a text menu

Regarding static solution, I agree with your teacher. This design makes handling nested menus virtually impossible. Once you consider submenus, it becomes obvious ...
vnp's user avatar
  • 56.5k
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,456
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.2k
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,408
5 votes
Accepted

Header only c++ singleton pattern implementation

I have a problem with this because template<class Type> struct SingleTon { static std::unique_ptr<Type> instance; This is still a static storage ...
Loki Astari's user avatar
  • 95.2k
5 votes

Client to connect to server and show a text menu

using System That first line of code makes it unnecessary to give the fully qualified class name. However, you always use ...
I'll add comments tomorrow's user avatar
5 votes
Accepted

Singleton Provider

I hope that code base isn't sprinkled with calls to that SingletonProvider! A "singleton provider", conceptually, makes no sense. This is highlighted by this ...
Mathieu Guindon's user avatar
5 votes
Accepted

ResourceManager class consisting of multiple singletons

The fact that you have "Manager" as part of the name of the class is a code smell. This class violates the Single Responsibility Principle. In particular, ...
200_success's user avatar
5 votes

An improved single instancing library

Just a very brief answer on one aspect. Objects that implement IDisposable generally have one of two behaviours when Dispose is ...
404's user avatar
  • 2,306
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
  • 43.4k
4 votes

Simple singleton database connection pool

Your connection pool is not threadsafe / concurrently usable, if you are sure you do not need it to be ignore my points 1. to 3. Your pool does not offer connection locking (mutual exclusive usage of ...
ASA's user avatar
  • 463
4 votes

ResourceManager class consisting of multiple singletons

The flag was raised here: public class ResourceManager { A FoobarManager class is like a ...
Mathieu Guindon's user avatar
4 votes

Storing global constants in app delegate or singleton

Storing everything in a global singleton can be problematic, I wouldn't recommend that. I worked on a project once where all data ever passed to view controllers from outside was stored a giant ...
iosdude's user avatar
  • 266
4 votes

Base class for singletons

Not a Singleton class Database extends Singleton { public function __construct(){ } } This is not a singleton. Add the following code: ...
mdfst13's user avatar
  • 21.6k
4 votes

Singleton implementation of a database connection

As per the docs https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.90).aspx Any public static (Shared in Visual Basic) members of this type are thread safe. Any ...
Wiktor Zychla'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
  • 715
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

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
  • 58.3k
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

Simple Publisher Subscriber in Java

Initializing object variables ...
mdfst13's user avatar
  • 21.6k
4 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

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
  • 58.3k
3 votes
Accepted

Unit-testing friendly singleton

I like the idea. First off, it appears that the get_instance() method isn't thread-safe: ...
Justin's user avatar
  • 3,295

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