singleton

Singleton Alternative - is it equivalent?

孤街浪徒 提交于 2020-01-15 05:31:09
问题 Quick question - I know that the standard singleton pattern is as follows: Original public class Singleton1 { public static Singleton1 _Instance; public static Singleton1 Instance { get { if (_Instance == null) { _Instance = new Singleton1(); } return _Instance; } } private Singleton1() { } } But it seems like this code is unnecessary. To me, you could accomplish the same thing with either of the following simple design patterns: Version 2 public class Singleton2 { public static readonly

Singleton Alternative - is it equivalent?

前提是你 提交于 2020-01-15 05:30:45
问题 Quick question - I know that the standard singleton pattern is as follows: Original public class Singleton1 { public static Singleton1 _Instance; public static Singleton1 Instance { get { if (_Instance == null) { _Instance = new Singleton1(); } return _Instance; } } private Singleton1() { } } But it seems like this code is unnecessary. To me, you could accomplish the same thing with either of the following simple design patterns: Version 2 public class Singleton2 { public static readonly

Binding “this” when passing an object's member function

眉间皱痕 提交于 2020-01-14 19:41:26
问题 I had a "class" defined and was making only one instance of it. The instance possessed a member function that would end up being passed around (it's a mouse handler, but that's not important). Since I would only ever make one instance of my "class", I decided to rewrite it as a singleton by using an object literal. So I have var mySingleton = { theObjects : []; } mySingleton.mouseHandler = (function() { var that = this; return function (e) { for (var indx = 0; indx < that.theObjects.length;

Can I define an abstract class for all derived Singletons in this way?

三世轮回 提交于 2020-01-14 07:06:07
问题 This is my abstract class which must be derived each time I want to make a Singleton: public abstract class Singleton<T> where T : Singleton<T> { private static readonly Lazy<T> _instance = new Lazy<T>(() => { var constructor = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null); return (T)constructor.Invoke(null); }); public static T Instance { get { return _instance.Value; } } public Singleton() { } } So, every time I need to follow the

Jersey 2 singleton dependency injection creates multiple instances

落爺英雄遲暮 提交于 2020-01-13 19:07:32
问题 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()

Does ASP.NET multithreading impact Structuremap singleton class?

你离开我真会死。 提交于 2020-01-13 16:25:12
问题 In my ASP.NET MVC project I have a class which is instantiated via Structuremap and is configured as a singleton. Given that ASP.NET is inherently multithreaded and the Singleton pattern is not threadsafe by default, will it cause any issues? I faced an issue where multiple instances were being returned for a class configured as Singleton. Could this problem be because of the instances being requested from different threads. EDIT : A more detailed description is given on this question

How can we create a Singleton Instance for a Window?

放肆的年华 提交于 2020-01-13 13:12:36
问题 I have searched for creating a Singleton object for a window in WPF. public static Test DefInstance { get { if (formDefInstance == null) // formDefInstance.IsDisposed { initializingDefInstance = true; formDefInstance = new cas18(); initializingDefInstance = false; } return formDefInstance; } set { formDefInstance = value; } } But the forDefInstance.IsDisposed is not working and throwing an error. Any Idea regarding this? 回答1: I think everyone should take a look at Jon Skeet's C# In Depth site

Java synchronize in singleton pattern

孤者浪人 提交于 2020-01-13 08:43:17
问题 Does the synchronize keyword need to be applied to each method of a class that implements the singleton pattern like this? public class Singleton { private Singleton(){} public synchronized static Singleton getInstance() { if(instance == null) instance = new Singleton (); return instance; } public void DoA(){ } } Since Singletons don't expose a public constructor and the getInstance() method is synchronized, one does not need to synchronize method DoA and any other public methods exposed by

When is it wise to use Singleton classes in Ruby? [closed]

余生长醉 提交于 2020-01-13 08:11:22
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I was reading about Singletons in Ruby. I never really had the need to use them before, but I got curious and decided to look them up,

When is it wise to use Singleton classes in Ruby? [closed]

ε祈祈猫儿з 提交于 2020-01-13 08:10:07
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I was reading about Singletons in Ruby. I never really had the need to use them before, but I got curious and decided to look them up,