可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have some confusion with singleton class, below are my some points:
- Can singleton class have static method?,if yes then how we call that methods?
- what is main difference between Static class and Singleton Class?
I have created my singleton class as follows:
public class Singleton
{
private static Singleton _instance = null;
private static object chekLock = new object();
private Singleton()
{}
public static Singleton Instance
{
get
{
lock (chekLock)
{
if (_instance == null)
_instance = new Singleton();
return _instance;
}
}
}
public static void StaticAddMethod()
{
Console.WriteLine("Add Method");
}
public void AddMethod()
{
Console.WriteLine("Add Method");
}
}
In Above class structure I have created two method one is Static and second is non static, When I am trying to access Static Method it gives me compile time error.
How can I use static method of singleton class?
回答1:
How are you trying to access it? To access a static method you use the type name:
Singleton.StaticAddMethod();
vs
Singleton.Instance.AddMethod();
Note also that there are simpler ways of implementing singletons that acheive the same effect with less locking etc.
Re the difference between singleton and static; a singleton might implement an interface, allowing you to pass it into existing code. You can also (as indeed you are doing) defer construction of a singleton (yet still allow access to the static methods that don't involve the singleton instance). But yes: there is a lot of crossover between static and singleton.
回答2:
You should be accessing the static method via:
Singleton.StaticAddMethod()
Or, if you are already inside Singleton
, then just:
StaticAddMethod()
The difference is that a static method is accessible from anywhere. An instance method requires that you invoke the method on a created instance of your class. With respect to a Singleton, it means that the class has instance fields/methods, but you can access the one-and-only instance via a static Instance
property.
回答3:
Static methods belong to the type itself, not to the type's instance. So you can call static method like this:
Singleton.StaticAddMethod()
回答4:
Static methods can be called from the Type directly. You dont need to create an instance to do so.
So
Singleton.StaticAddMethod();
Will work
Also change your declaration from
private static Singleton _instance = null;
to
private static READONLY Singleton _instance = new Singelton();
This will allow you to get rid of all the locking you are doing, since the readonly can only be assigned on creation, protecting you from any locking you could run into ;)
public static Singleton Instance
{
get
{
return _instance;
}
}
回答5:
Like this:
Singleton.StaticAddMethod();
Singleton.Instance.AddMethod();
回答6:
To your second question, one difference between the two is that a singleton class can be sublclassed and participate in polymorphic behavior in a way that a static class cannot.
回答7:
Here is an example of how to access members of a Singleton Class which is thread safe, and how to code that class.
http://www.helplessautomation.com/2011/01/creating-a-singleton-in-c-instance-class/
El Matador