singleton

Is it bad practice to have state in a static class?

∥☆過路亽.° 提交于 2019-12-08 15:24:21
问题 I would like to do something like this: public class Foo { // Probably really a Guid, but I'm using a string here for simplicity's sake. string Id { get; set; } int Data { get; set; } public Foo (int data) { ... } ... } public static class FooManager { Dictionary<string, Foo> foos = new Dictionary<string, Foo> (); public static Foo Get (string id) { return foos [id]; } public static Foo Add (int data) { Foo foo = new Foo (data); foos.Add (foo.Id, foo); return foo; } public static bool Remove

Example of Singleton pattern

梦想的初衷 提交于 2019-12-08 14:52:26
问题 Can anybody tell me a good example of Singleton pattern? Also I've one doubt to ask, Is the following scenario is that of singleton pattern: When we have many printers connected in LAN but only one printer queue? 回答1: Singleton is a software pattern. Here is an example in C#. Having a single queue on a LAN is more of a hardware/network design issue rather than a software concept, so not really applicable. If you were modeling such a thing in software and had to be certain there is only one

Singleton compact Logger for ASP.NET applications

让人想犯罪 __ 提交于 2019-12-08 12:22:32
问题 I have coded a Singeton compact logger which is very handy for ASP.NET applications. Just refrencing it and then Logger.Log.Info("Hello world!"); . It also logs unhandled exceptions automatically. Sometimes I get error where I try to create the log file Stream . The exception is: The process cannot access the file 'C:\inetpub\wwwroot\MyApp\Logs\5-22-2011.log' because it is being used by another process.. I checked with Process Explorer and only w3wp.exe has handle over the log file. It seems

Thread safe singleton in C++

左心房为你撑大大i 提交于 2019-12-08 11:26:39
问题 I have been reading about thread safe singletons and the implementation I find everywhere has a getInstance() method something like this: Singleton* getInstance() { if ( !initialized ) { lock(); if ( !initialized ) { instance = new Singleton(); initialized = true; } unlock(); } return instance; } Is this actually thread safe? Have I missed something or is there a small chance this function will return an uninitialized instance because 'initialized' may be reordered and set before instance?

Connection Manager: Singleton or not Singleton?

心不动则不痛 提交于 2019-12-08 11:21:33
My iOS app does a lot of different requests to a Web service. Each request is a call to a method of a ConnectionManager object. When the response arrives from the Web service, a delegate's method is called to notify an interested receiver. Moreover, to maintain the session active, a polling every X seconds is required. Said so, in your opinion it is better if ConnectionManager is a Singleton or not? The singleton is simpler (because I do not have to pass a ConnectionManager's reference to all those who need to do a request to the Web service or I do not have to create more ConnectionManagers).

SignalR connection wouldn't start inside a singleton class

百般思念 提交于 2019-12-08 10:07:08
问题 I have setup my signalR connection inside a singleton class so I can use the same connection throughout my entire project. The problem is that the connection never starts and the code never executes beyond the line await hubConnection.Start() however when I do this outside the single class, then the connection is initiated in an instant. I wonder what I'm doing wrong. Here's my definition of the singleton class: public sealed class ProxySubscriber { private static volatile ProxySubscriber

Running only one background thread in google appengine backend

…衆ロ難τιáo~ 提交于 2019-12-08 09:27:21
问题 As mentioned in documentation I am running a background thread in a backend with 1 instance infinitely for some continuous background processing. import com.google.appengine.api.ThreadManager; import java.util.concurrent.AtomicLong; AtomicLong counter = new AtomicLong(); Thread thread = ThreadManager.createBackgroundThread(new Runnable() { public void run() { try { while (true) { counter.doStuff() Thread.sleep(10); } } catch (InterruptedException ex) { throw new RuntimeException("Interrupted

Make Singleton class in Multiprocessing

非 Y 不嫁゛ 提交于 2019-12-08 08:48:23
问题 I create Singleton class using Metaclass , it working good in multithreadeds and create only one instance of MySingleton class but in multiprocessing, it creates always new instance import multiprocessing class SingletonType(type): # meta class for making a class singleton def __call__(cls, *args, **kwargs): try: return cls.__instance except AttributeError: cls.__instance = super(SingletonType, cls).__call__(*args, **kwargs) return cls.__instance class MySingleton(object): # singleton class _

How can I implement a Singleton class that can be derived from in WPF?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 08:15:02
问题 Some time ago I learned of the Singleton implementation that only permits a single instance of a class object by hiding the class initializer and using a private static reference of the object within itself, and a public GETTER that references that private reference - public class Foo : IDisposable{ private static Foo _Instance; public static Foo Instance{ get{ return Foo._Instance ?? new Foo(); } private Foo(){ Foo._Instance = this; } public void Dispose(){ Foo._Instance = null; } } I love

Establishing database connection in php using singleton class

核能气质少年 提交于 2019-12-08 08:04:28
问题 Can anybody please guide me with a sample code to establish a database connection in php using singleton class. 回答1: class DatabaseSingleton { // [Singleton] private static $instance = null; public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } private function __clone(){} // [/Singleton] private $connection = null; private function __construct() { $this->connection = mysql_connect('localhost','root','admin'); if ($this-