21 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.5k
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,909
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
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
7 votes

Generic singleton array with archiving and insertion functions

As my comment explains, there are some things about your code I definitely don't understand. This answer will focus on the parts that I know need to be changed. So, starting from the top... ...
nhgrif's user avatar
  • 25.2k
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,260
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
  • 55.9k
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

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,458
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,313
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

Android app with config and customization classes

Public class, private Interface? I'm pretty confused by this setup. The meaning is always trying working towards the most highest object( this case interface) possible. I'm thinking that you created ...
chillworld's user avatar
  • 3,872
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

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 ...
Martin York's user avatar
  • 94.3k
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
  • 42.7k
4 votes
Accepted

Loading Configurations from plist into singleton

I don't have a whole lot to add, as this is pretty straightforward and seems like a completely reasonable way to do what you're trying to do. In regards to your questions: There's a tradeoff for ...
user1118321's user avatar
  • 11.6k
4 votes
Accepted

(Y.E.S.E.J.) Yet Another Singleton with Enum in Java

Despite some other comments, I believe there are times when a singleton is very appropriate, and a powerful tool. The best practice, in Java, since the introduction of enums, is to use them for ...
rolfl's user avatar
  • 97.3k
4 votes
Accepted

Initialization lock for singleton

As vnp points out, your code is incorrect because a thread may read REFERENCE after IS_CREATED is set but before ...
Reinstate Monica's user avatar
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
  • 473
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.4k
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
  • 50.5k

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