All Questions

1647
votes
36answers
375932 views

What is so bad about singletons?

The singleton pattern is a fully paid up member of the GoF's patterns book, but it lately seems rather orphaned by the developer world. I still use quite a lot of singletons, especially for factory cl...
684
votes
30answers
272918 views

What is an efficient way to implement a singleton pattern in Java?

What is an efficient way to implement a singleton pattern in Java?
495
votes
16answers
478914 views

C++ Singleton design pattern

Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real life example): // a lot of methods are omitted...
552
votes
17answers
287840 views

How to declare global variables in Android?

I am creating an application which requires login. I created the main and the login activity. In the main activity onCreate method I added the following condition: public void onCreate(Bundle savedI...
334
votes
26answers
181041 views

What should my Objective-C singleton look like?

My singleton accessor method is usually some variant of: static MyClass *gInstance = NULL; + (MyClass *)instance { @synchronized(self) { if (gInstance == NULL) gInstance ...
124
votes
11answers
35174 views

Is there a use-case for singletons with database access in PHP?

I access my MySQL database via PDO. I'm setting up access to the database, and my first attempt was to use the following: The first thing I thought of is global: $db = new PDO('mysql:host=127.0.0.1;...
1381
votes
34answers
362431 views

Difference between static class and singleton pattern?

What real (i.e. practical) difference exists between a static class and a singleton pattern? Both can be invoked without instantiation, both provide only one "Instance" and neither of them is thread-...
507
votes
28answers
114214 views

Using a dispatch_once singleton model in Swift

I'm trying to work out an appropriate singleton model for usage in Swift. So far, I've been able to get a non-thread safe model working as: class var sharedInstance:TPScopeManager { get { ...
12
votes
3answers
6846 views

Setting Singleton property value in Firebase Listener

I'm currently testing out Firebase along with a Singleton model I plan to use to access during the lifecycle of the whole app. I'm now stuck with something that seems really trivial but I can't figure...
319
votes
10answers
126811 views

Singletons vs. Application Context in Android?

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it's a good idea to use Singletons in...

Previous Next