singleton

singleton - trying to initialise a static property on creation fails

放肆的年华 提交于 2019-12-20 05:45:37
问题 I have a singleton class used to initialise error_handling. The class as is takes a Zend_Config object and optional $appMode in parameter, to allow overriding the defined APPMODE constant when testing this class. All is fine if I create the object with non-static properties, but initialising a static property does not work the way I expected when calling the usual getInstance(). class ErrorHandling{ private static $instance; private static $_appMode; // not initialised in returned instance

explain java compile order

廉价感情. 提交于 2019-12-20 05:27:32
问题 v2 is null when it enters A's c'tor at the first time, but if I put v2 's declaration & initialization before instance it will have a value; why is this? public class A { private static final String v1 = new String(new byte[]{'a', 'b'}); private static A instance = new A(); private static final String v2 = new String(new byte[]{'b', 'c'}); private A() { System.out.printf("A c'tor\tv1 [%s]\tv2 [%s]\n", v1, v2); } public static void main(String[] args) { System.out.println("start main"); new A(

Using Static constructors to create a singleton in C#

若如初见. 提交于 2019-12-20 05:11:54
问题 I've been looking at the article on creating static constructors in c# here: http://csharpindepth.com/Articles/General/Singleton.aspx Now, one option he doesn't mention is using a simple static constructor. Is there any issue with doing something like the below? If it works, it seems simpler than his complicated solutions IMHO. public static class ServiceFactory { static ServiceFactory() { container = new Foo(); } static Foo container; public static Instance { get { return container; } } }

In dotnet core how can I ensure only one copy of my application is running?

↘锁芯ラ 提交于 2019-12-20 04:15:08
问题 In the past I have done something like this private static bool AlreadyRunning() { var processes = Process.GetProcesses(); var currentProc = Process.GetCurrentProcess(); logger.Info($"Current proccess: {currentProc.ProcessName}"); foreach (var process in processes) { if (currentProc.ProcessName == process.ProcessName && currentProc.Id != process.Id) { logger.Info($"Another instance of this process is already running: {process.Id}"); return true; } } return false; } Which has worked well. In

Static variables in Python that are shared across files

谁都会走 提交于 2019-12-20 03:21:59
问题 I am pretty new to Python and I could't find a way to have static variables that are shared across different python files... In Java, it seems quite straight forward to do so... Here is what I'm trying to do: data.py class Shared(object): list_vars = [] var = "" @staticmethod def print_vars(): print "list_vars = " + str(Shared.list_vars) print "var = " + Shared.var user1.py import time from data import Shared Shared.list_vars.append("001") time.sleep(5.0) Shared.print_vars() # It prints: #

Singleton in a DLL?

走远了吗. 提交于 2019-12-20 03:07:42
问题 So i am trying to export somethings in a project to DLL. Anyways a few of the projects use a singleton class very heavily. template <typename T> class DLL_IMP VA_Singleton { protected: VA_Singleton () {}; ~VA_Singleton () {}; public: static T *Get(){ return (static_cast<T*> (a_singleton)); } static void Delete(){ if(a_singleton == NULL) { delete a_singleton; } } static void Create(){ a_singleton = GetInstance(); if(a_singleton == NULL){ a_singleton = new T; } } private: static T *a_singleton;

Initialize-On-Demand idiom vs simple static initializer in Singleton implementation

可紊 提交于 2019-12-20 02:32:38
问题 Is the Initialize-On-Demand idiom really necessary when implementing a thread safe singleton using static initialization, or would a simple static declaration of the instance suffice? Simple declaration of instance as static field: class Singleton { private static Singleton instance=new Singleton(); private Singleton () {..} public static Singleton getInstance() { return instance; } } vs class Singleton { static class SingletonHolder { static final Singleton INSTANCE = new Singleton(); }

Are Java Class objects unique / singletons? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-20 02:05:19
问题 This question already has answers here : Does Java guarantee that Object.getClass() == Object.getClass()? (4 answers) Closed 3 years ago . are Class-objects guaranteed to be unique in the JVM, are they Singletons? i.e. will getClass() == getClass() always hold true and is a == b true if and only if a.equals(b) where a and b are of type Class<?> ? 回答1: You can have one singleton and/or class per class loader. a.equals(b) is required to be true if a == b (except if a is null ) 来源: https:/

Are Java Class objects unique / singletons? [duplicate]

拜拜、爱过 提交于 2019-12-20 02:05:04
问题 This question already has answers here : Does Java guarantee that Object.getClass() == Object.getClass()? (4 answers) Closed 3 years ago . are Class-objects guaranteed to be unique in the JVM, are they Singletons? i.e. will getClass() == getClass() always hold true and is a == b true if and only if a.equals(b) where a and b are of type Class<?> ? 回答1: You can have one singleton and/or class per class loader. a.equals(b) is required to be true if a == b (except if a is null ) 来源: https:/

in C++, how to use a singleton to ensure that each class has a unique integral ID?

微笑、不失礼 提交于 2019-12-20 01:38:18
问题 I have a bunch of C++ classes. I want each class to have something like: static int unique_id; All instances of a same class should have the same unique_id; different classes should have different unique_id's. The simplest way to do this appears to be threading a singleton through the classes. However, I don't know what's called when for static class members / things that happen before main. (1) if you have a solution that does not involve using singleton, that's fine too (2) if you have a