singleton

Dagger 2 - how to create/provide a EagerSingleton

时间秒杀一切 提交于 2019-12-10 12:47:14
问题 I am having trouble with the Dagger 2 dependency injection framework. I would like to create an EagerSingleton . I assume that dagger 2 creates lazy loaded singletons when I use the @Singleton annotation. How do I create EagerSingleton using the Dagger 2 framework ? 回答1: I solved this by creating an EagerModule which had a single provide method which returned Void . Everything I wanted created eagerly I specified as parameters to that method. Then I added a Void init() method to the Component

Non-UI Fragment vs Singleton

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:42:23
问题 I guess that the main purpose of the non-UI fragments is the storage of data that is retained over configuration changes, right? So, appart from being this storage specific for the Activity that owns this fragment, which is the benefit of its usage over a Singleton pattern across the entire application (which is the solution I've been doing so far)? 回答1: The fact that a fragment is scoped to its activity means there is less chance of a long-term memory leak, as opposed to singletons -- the

Which kind of Singleton is this?

隐身守侯 提交于 2019-12-10 12:29:21
问题 public class ConnectionManager { private static Map <String, ConnectionManager> managerInstances = new HashMap<String, ConnectionManager>(); private String dsKey; private ConnectionManager(String dsKey){ this.dsKey = dsKey; } public static ConnectionManager getInstance(String dsKey){ ConnectionManager managerInstance = managerInstances.get(dsKey); if (managerInstance == null) { synchronized (ConnectionManager.class) { managerInstance = managerInstances.get(dsKey); if (managerInstance == null)

Sharing data/string with a singleton between views

寵の児 提交于 2019-12-10 12:17:00
问题 I'm trying to share a string between two views on my iPhone project. It currently works if I use the actual @"something here" for the string, but if I want to use something like label.text, it doesn't even though it is still a string. I'll show you what I have to make it clearer. First View: Info_ViewController.h #import <UIKit/UIKit.h> @interface Info_ViewController : UIViewController { IBOutlet UITextField *locationField; } @property (nonatomic, retain) NSString *locationString; + (id

Java fine-grained synchronization in getter/setter methods and singleton pattern of a class

醉酒当歌 提交于 2019-12-10 11:59:53
问题 I'm trying to use the synchronization java directive to implement fine-grained synchronization in a class, i.e. synchronize the least amount of code I can.. I'll comment the code inline, to explain what I do and after the code I'll ask you how to improve the code: public class MyClass { private static volatile MyClass singletonInstance = null; private HashMap<String, Integer> mHashMap = null; private String mStringA = null; private String mStringB = null; // Use double check technique to use

How we place Phoenix Singleton on same address? C++

谁说胖子不能爱 提交于 2019-12-10 11:47:53
问题 below code illustrates the Phoenix Singleton described in Andrey Alexandrescu's Modern C++ Design book. Singleton& Instance() { if (!pInstance_) { // Check for dead reference if (destroyed_) { OnDeadReference(); } else { // First call—initialize Create(); } } return pInstance_; } void Singleton::OnDeadReference() { // Obtain the shell of the destroyed singleton Create(); // Now pInstance_ points to the "ashes" of the singleton // - the raw memory that the singleton was seated in. // Create a

Referencing an instantiated object in a static class (c#)

半世苍凉 提交于 2019-12-10 11:32:44
问题 I am wondering if it is possible to have a static class instantiate another class for the purpose of holding a reference to it globally. I have a data store which is composed of an in-memory object and would like to access it from different locations. The data needs to persist changes to the application so it needs to be instantiated outside of window or UI scope. I was hoping that using a static class to do this would be the correct way of doing it. Is this what a singleton is? Is what I am

Java: Lazy loading Singleton and reflection attack?

谁说胖子不能爱 提交于 2019-12-10 11:25:18
问题 If I implement a Singleton either through holder idiom or double checked locking, but instead of calling 'getInstance()', use reflection to instantiate it, and then call 'getInstance()' on it, this would create two instances, breaking the pattern. So I add a static 'counter' member to the class, increment it in the class's private constructor, and throw an exception if it crosses '1'. But in that case, if I first instantiate through reflection, nobody else would be able to call 'getInstance()

Write to a single log file in vbscript

百般思念 提交于 2019-12-10 11:19:26
问题 I have written a small sub to create log file sub WriteLogFileLine(sLogFileNameFull, sLogFileLine) logfolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder\" ScriptName1 = Replace(Wscript.ScriptName, ".vbs", "") sLogFileName = ScriptName1 & "_" & date & "_" & hour(now) & "-" & minute(now) & "-" & second(now) & "_log.txt" sLogFileNameFull = logfolder & sLogFileName dateStamp = Now() Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True) MyLog.WriteLine(cstr(dateStamp) + vbTab + "-" + vbTab +

How to define a class variable on a singleton class

点点圈 提交于 2019-12-10 10:54:47
问题 I want to define a class variable on a singleton class. I checked this program's result: class C class << self @@val = 100 end end C.singleton_class.class_variables #=> [], I expect [:@@val] C.class_variables #=> [:@@val] I expect the scope of @@val to be the singleton class, isn't it? Would you tell me how to define a class variable on a singleton class using class << self , and the reason why this program is not correct? 回答1: It is because when Ruby parser meets a class variable, the