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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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-...
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 ...
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 ...
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/...
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 ...
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 <...
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,...
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 ...
4
votes
Accepted
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 "...
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 ...
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 ...
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 ...
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:
...
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....
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 ...
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:
...
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 ...
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:
<...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
singleton × 177java × 52
c# × 39
c++ × 31
object-oriented × 23
design-patterns × 20
php × 18
thread-safety × 17
android × 11
multithreading × 9
c++11 × 8
ios × 8
objective-c × 8
javascript × 7
static × 7
python × 6
beginner × 6
swift × 6
dependency-injection × 6
ruby × 5
template × 5
concurrency × 5
performance × 4
c++17 × 4
logging × 4