singleton

What is the proper way to initialize singleton element in a thread safe way in Scala?

為{幸葍}努か 提交于 2019-12-11 12:34:34
问题 I've faced a problem of initializing singleton element in a thread safe way in Scala. Usually companion object is used. But this time I need to pass a config object to initializer. Calling some init function of companion object either not thread safe, or leads to locks, that looks kind of low level for Scala (may be I'm not right). The thing I came up with is a cache. Calling cache with the same argument helps to initialize it exactly once. But the strange thing for me is that I have to use

Xcode/Swift error: Command failed due to signal: Segmentation fault: 11

送分小仙女□ 提交于 2019-12-11 11:18:09
问题 I am trying to call a function of a singleton class with completion handler argument but I get "Command failed due to signal: Segmentation fault: 11" error. I am using Xcode 6.2 (6C101) and trying to build for iOS 8 on iPhone 6 simulator. Here is the singlton class: public class ClientManager { public class var sharedInstance: ClientManager { struct Singleton { static let instance = ClientManager() } return Singleton.instance } private init() { } public func fetchServiceInfo(serviceName:

does __init__ get called multiple times with this implementation of Singleton? (Python)

与世无争的帅哥 提交于 2019-12-11 11:15:18
问题 Source: Python and the Singleton Pattern According to most upvoted comment in the top answer init gets called multiple times if new returns class instance. So I checked this: class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): print 'Singleton.__new__ called with class', cls if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instance class Cache(Singleton): def __init__(self, size=100): print 'I am called with size',

Singleton with two getInstance() methods handing over a parent pointer?

坚强是说给别人听的谎言 提交于 2019-12-11 11:07:01
问题 I am still working on my Logger and I like the idea of a Singleton but my Logger derives frm QDialog, thus I would like to handle my Parent QWidget* MainWindow pointer when I call it first: class Logger : public QDialog { Q_OBJECT private: explicit Logger(QWidget* parent = 0); public: static Logger& firstInstance(QWidget* parent = 0) { static Logger theInstance(parent); return theInstance; } static Logger& instance() { return theInstance; } //.. } So I would call Logger::firstInstance(this);

What are the purposes of other members of a Singleton class besides the instance and its get method?

让人想犯罪 __ 提交于 2019-12-11 10:48:38
问题 From Design Pattern by GoF Participants • Singleton defines an Instance operation that lets clients access its unique instance uniqueinstance . Instance is a class operation (that is, a class method in Smalltalk and a static member function in C++). may be responsible for creating its own unique instance uniqueinstance . Collaborations • Clients access a Singleton instance uniqueinstance solely through Singleton 's Instance operation. In Class Singleton , uniqueinstance is the unique instance

Receiving dynamically changing classes

拟墨画扇 提交于 2019-12-11 09:57:57
问题 In my system I have 16 different classes alike used for statistics. They look like the following public class myClass : myInheritage { private static myClass _instance; public static myClass Instance { get { return _instance ?? (_instance = new myClass(); } } public static void Reset() { _instance = null; } } They are all made into singletons myInheritage looks like this: public class myInheritage { int data = 0; public myInheritage() { } public int Data { get { return data; } set { data+=

How to determine if `this` is an instance of a class or an object?

Deadly 提交于 2019-12-11 09:38:40
问题 Suppose I have two descendants of an abstract class: object Child1 extends MyAbstrClass { ... } class Child2 extends MyAbstrClass { } Now I'd like to determine (preferably in the constructor of MyAbstrClass ) if the instance being created is an object or something created by new : abstract class MyAbstrClass { { if (/* is this an object? */) { // do something } else { // no, a class instance, do something else } } } Is anything like that possible in Scala? My idea is to collect all objects

IOS Weird compiler error: "expected ']' before ;' using set-ter for NSString constant in singleton

走远了吗. 提交于 2019-12-11 07:48:58
问题 I have the weirdest error! In Xcode I have a singleton with the following defined (file is: MyGizmoClass.h ): NSString *plistPath; NSString *dataDomain; NSString *pathToChatScript; NSString *pathToUpdates; and @property (nonatomic,retain) NSString *plistPath; @property (nonatomic,retain) NSString *dataDomain; @property (nonatomic,retain) NSString *pathToChatScript; @property (nonatomic,retain) NSString *pathToUpdates; I have a Constants.h file (which I #import early in my .pch file) that

Why the default constructor executed before the static constructor?

浪尽此生 提交于 2019-12-11 07:45:57
问题 I am wondering why my static constructor is outputting default constructor Static Constructor , and not the other way around Static Constructor and Default constructor or just Default constructor . When I use a static constructor, it should execute the static constructor first. However, from the code below, The First question: why is the default constructor is called before the static constructor? class Program { static void Main(string[] args) { var test = Single.S; } class Single{ static

Win Forms Global Exception Handling

浪尽此生 提交于 2019-12-11 07:22:17
问题 i'm trying to do globalexceptionhandling, i've already tried 3 methods to do this but nothing caught an exception thrown somewhere in the program static void Main() { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Catch); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Catch);