开发者

Difference between static class and singleton class c# [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Difference between static class and singleton pattern?

we use static class for common operation. the same thing can be done singleton class also

here i am giving two class one is static class and one is singleton class. actually things is getting harder for me that when one should 开发者_运维技巧go for static class and when we should go for singleton class.

public sealed class SiteStructure
{
    /// <summary>
    /// This is an expensive resource we need to only store in one place.
    /// </summary>
    object[] _data = new object[10];

    /// <summary>
    /// Allocate ourselves. We have a private constructor, so no one else can.
    /// </summary>
    static readonly SiteStructure _instance = new SiteStructure();

    /// <summary>
    /// Access SiteStructure.Instance to get the singleton object.
    /// Then call methods on that instance.
    /// </summary>
    public static SiteStructure Instance
    {
    get { return _instance; }
    }

    /// <summary>
    /// This is a private constructor, meaning no outsiders have access.
    /// </summary>
    private SiteStructure()
    {
    // Initialize members, etc. here.
    }
}

static public class SiteStatic
{
    /// <summary>
    /// The data must be a static member in this example.
    /// </summary>
    static object[] _data = new object[10];

    /// <summary>
    /// C# doesn't define when this constructor is run, but it will likely
    /// be run right before it is used.
    /// </summary>
    static SiteStatic()
    {
    // Initialize all of our static members.
    }
}

please explain when one need to create static class and when singleton class. thanks


My opinion is:

Static class you need to create a class which will operate to you like API base, so basically it's just functions or constants set. For the user of your class it's being static signals about the lack of the state, basically.

Singleton is just a class whom instance can be only one, and the case that you provide static property and private default constructor (cause that what you should do for not letting to your class user create objects) it's just consiquence of design.

Hope this helps.

Regards.


You use a singleton when you want to instantiate a class into an object, but you only want one object at a time.

A static class does not instantiate into an object.


Both the classical (Class.Instance) singletons and classes with static mutable state are almost equally bad IMO. There is rarely a use for either.

Static classes are nice for helper functions that access no state, such as Math or Enumerable.

My preferred alternative for singletons with state is injecting singletons via dependency injection. That way you don't build your code around the assumption that something is a singleton. But there just happens to be a single instance. So changing your code if you require more than one in the future is easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜