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 instead of single instances shared through global application state (subclassing android.os.Application and obtaining it through context.getApplication()). What advantages/drawbacks would both mechanisms have? To be honest, … Read more

Simplest/cleanest way to implement a singleton in JavaScript

What is the simplest/cleanest way to implement the singleton pattern in JavaScript? 40 Answers 40 I think the easiest way is to declare a simple object literal: var myInstance = { method1: function () { // … }, method2: function () { // … } }; If you want private members on your singleton instance, … Read more

Is there a simple, elegant way to define singletons? [duplicate]

This question already has answers here: Creating a singleton in Python (33 answers) Closed 3 years ago. There seem to be many ways to define singletons in Python. Is there a consensus opinion on Stack Overflow? 2Best Answer 21 Here’s my own implementation of singletons. All you have to do is decorate the class; to get … Read more

On design patterns: When should I use the singleton?

The glorified global variable – becomes a gloried global class. Some say breaking object-oriented design. Give me scenarios, other than the good old logger where it makes sense to use the singleton. 22 s 22 On my quest for the truth I discovered that there are actually very few “acceptable” reasons to use a Singleton. … Read more

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 { struct Static { static var instance: TPScopeManager? = nil } if !Static.instance { Static.instance = TPScopeManager() } return Static.instance! } } Wrapping … Read more

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 savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); … loadSettings(); if(strSessionString == null) { login(); } … } The onActivityResult method which is executed when the login form terminates … Read more

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

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago. Improve this question What is an efficient way to implement a singleton design pattern in Java? 29 s … Read more

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 here class Singleton { public: static Singleton* getInstance( ); ~Singleton( ); private: Singleton( ); static Singleton* instance; }; From this declaration, I … Read more