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 ...
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 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 ...
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...
...
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 ...
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 ...
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 ...
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
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 ...
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
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, ...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
4
votes
ResourceManager class consisting of multiple singletons
The flag was raised here:
public class ResourceManager {
A FoobarManager class is like a ...
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 ...
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:
...
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 ...
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
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
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,...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
singleton × 179java × 52
c# × 39
c++ × 31
object-oriented × 23
design-patterns × 20
php × 18
thread-safety × 17
android × 11
javascript × 9
multithreading × 9
c++11 × 8
ios × 8
objective-c × 8
static × 7
python × 6
beginner × 6
swift × 6
dependency-injection × 6
ruby × 5
template × 5
concurrency × 5
performance × 4
c++17 × 4
logging × 4