C# Singleton Pattern -


I use singleton patterns in many places, sometimes the constructor does nothing, for the second time these things Getting Started I was surprised if my code was a way to set an abstract class to reduce duplication, i.e. single single statistic for public reading; In each class, only one base class I understand that the interface is not an option.

I have tried to use the following (taken from);

  Public abstract class singleton & lt; T & gt; Where T: new () {static singleton () {} private static readonly T _ instance = new T (); Example of public static T ({return_instance;}}}  

The problem with this is that I can not override the manufacturer for those cases where I need to start things Is I trying to make it possible too? Or is it just what I am doing, can not I worry about a Best Singleton Class?

I wonder if my code repeats There was a way to set an abstract class to reduce the value

No, not as soon as you got an abstract class, you have a class that can be instantly instantiated I can see many people try to do something like this , but the end result is neither a singleton or more complicated than what you are already doing.

Am I trying to make it possible too? Or am I just doing what I am doing Yes and do not be worried about a single single class?

You should not try to build the base singleton class - but if I were you, so many songs in the first place Try to stop using the singleton pattern is very easy to use, and it is similar to an anti-pattern. See whether you can refinance against dependency injection using one of these configurations, which creates only one example from all these classes, but where it is a matter of configuration rather than applied by itself.

Unlike anything else, it is a pain to have a single test, just because it is a global state, which requires cleaning between tests and so on.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -