singleton

Is Lazy<T> a good solution for a thread safe lazy loaded singleton?

拟墨画扇 提交于 2019-12-06 19:04:08
问题 We implemented a lazy loaded singleton using double locking on get to make sure the instance is only initialized once (and not twice due to thread race conditions). I was wondering if simply using Lazy<T> is a good solution for this problem? I.E. private static Lazy<MyClass> _instance = new Lazy<MyClass>(() => return new MyClass()); public static MyClass Instance { get { return _instance.Value; } } 回答1: I suggest you to read referencede articles from comments: Lazy Class Implementing the

Binding To Singleton Class Observable Collection Member

自作多情 提交于 2019-12-06 19:01:08
问题 I just can't seem to figure this out. I found some similar Questions here but either I can't figure out the right direction for my approach or I am doing something completly wrong. My Application has a Singleton Class Logger, which saves Log messages from every class in my program. public class Logger { private Logger() { } private static volatile Logger instance; public static Logger GetInstance() { // DoubleLock if (instance == null) { lock (m_lock) { if (instance == null) { instance = new

Objective-C: Use singleton vs. use class as an object?

懵懂的女人 提交于 2019-12-06 17:55:43
问题 I've been wondering in what cases it is really necessary to adopt the singleton pattern in objective-C (e.g., define a dedicated class and create a single instance), that using the class as an object won't do. Particularly, I'm thinking of the following solution: Define and use appropriate class methods, instead of instance methods on the singleton instance; Use static variables (file-scope globals), instead of instance variables of the singleton instance; Use the class object when

Android Volley Singleton Pattern how to add/cancel requests based on tag

半世苍凉 提交于 2019-12-06 17:02:53
问题 According to https://developer.android.com/training/volley/requestqueue.html#singleton it is discouraged to use the old way of implementing a singleton class by setting up the RequestQueue in Application.onCreate() The provided "new" more modular way as seen below however doesn't contain a method for adding tags to requests and cancelling them using these tags. public class MySingleton { private static MySingleton mInstance; private RequestQueue mRequestQueue; private ImageLoader mImageLoader

extending a Singleton class?

落花浮王杯 提交于 2019-12-06 15:16:25
问题 Is there a way to make like a template singleton class so that when you extend it, the extended class is also a singleton class, so I can quickly make a new singleton class just my extending the template singleton class? I am using ActionScript 3.0 if it matters. Thanks. 回答1: The singleton pattern is designed in such a way that the type ought not to be inherited. I am not familiar with ActionScript but having an singleton type that is also able to be extended seems like a mistake. If you were

How to make a single shared instance of iAd banner throughout many view controllers?

◇◆丶佛笑我妖孽 提交于 2019-12-06 15:11:42
I have tried using a singleton class in my app delegate but I haven't been able to get that to work. I've also checked out the iAdSuite examples (particularly the containerBanner example because it seemed to be the most relative) but I can't figure it out. If there's a better way to accomplish this without using a singleton class and you can point me in the right direction I'd really appreciate it. Some of my singleton class code is below. Thank you! @interface App Delegate @property (assign) iAdController *iadc; + (AppDelegate*) sharedApplication; - (iAdController*)sharedAd; @end

How do I debug singletons in Objective-C

家住魔仙堡 提交于 2019-12-06 14:21:39
My app contains several singletons (following from this tutorial ). I've noticed however, when the app crashes because of a singleton, it becomes nearly impossible to figure out where it came from. The app breakpoints at the main function giving an EXEC_BAD_ACCESS even though the problem lies in one of the Singleton objects. Is there a guide to how would I debug my singleton objects if they were problematic? Hot Licks I scanned the article, and while it had some good ideas it also had some bad advice, and it should not be taken as gospel. And, as others have suggested, if you have a lot of

Jersey 2 singleton dependency injection creates multiple instances

£可爱£侵袭症+ 提交于 2019-12-06 13:44:20
Here I have a singleton, that I whant to inject to my application @Singleton @Path("singleton-bean") public class MyContext { private MyContext() { instances++; } private static MyContext instance; public static MyContext getInstance(){ if (instance == null) instance = new MyContext(); return instance; } public static int instances = 0; } Here's how I register it: @ApplicationPath("webresources") public class ApplicationConfig extends Application { @Override public Set<Object> getSingletons() { final Set<Object> singletons = new HashSet<>(); singletons.add(MyContext.getInstance()); return

Singleton in Objective-C, compatible with ARC and thread safe

醉酒当歌 提交于 2019-12-06 13:37:15
问题 I want to have a thread safe, ARC compatible singleton, but is seems to me that the most common example of singleton that I find, an example pasted here: + (MyClass *)sharedInstance { static MyClass *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; // Do any other initialisation stuff here }); return sharedInstance; } doesn't stops other developer from calling [[MyClass alloc] init] and overriding the desired flow. What

Why Can Singleton classes be used as regular classes

佐手、 提交于 2019-12-06 13:27:42
问题 I was under the impression that the main reason for using singletons was to make sure that only one instance could be created in a program. I thought that the compiler wouldn't let you create instances of a singleton as if it would be a regular class. In the following code I have a singleton where I'm creating multiple instances of it and it behaves as a regular class, but for some reason I was expecting an error. What makes a singleton different than a regular class if it lets you create