简体   繁体   English

静态类与类实例

[英]Static class vs. Instance of the class

I have a static class which I use to get access to my public properties (global for whole app) and methods which I use during application run. 我有一个静态类,可用于访问我的公共属性(整个应用程序是全局的)和在应用程序运行期间使用的方法。 For example I set some property in static class and during app run time I can get value from property. 例如,我在静态类中设置了一些属性,在应用运行时,我可以从属性中获取价值。

But I can create non static class with singleton pattern and use it in the same way. 但是我可以使用单例模式创建非静态类,并以相同的方式使用它。

Question: Which approach is correct in my case? 问题:哪种方法对我来说是正确的?

Depends on what you're trying to achieve. 取决于您要实现的目标。

I'd go for static classes to provide utility functions in your application, and mostly avoid singletons for this type of thing. 我会选择静态类在您的应用程序中提供实用程序功能,并且大多数情况下都避免单例。 See this question for info about when to use singletons. 这个问题对于有关何时使用单身的信息。

If your class represents some sort of entity in the system (Example: A user, a blog post, a product, a student etc.) it should not be a static class, but be instantiated every time you are logically using a separate instance of it. 如果您的班级代表系统中的某种实体(例如:用户,博客文章,产品,学生等),则该班级不应是静态班级,而应在每次逻辑上使用单独的实例时实例化。它。

The sample below shows that you can use interfaces with a singleton class (which is impossible with static class.) 下面的示例显示可以将接口与单例类一起使用(对于静态类是不可能的)。

I prefer this pattern above a large list of static methods/properties or several static classes. 在大量静态方法/属性或几个静态类上方,我更喜欢这种模式。

Those interfaces can provide subject specific settings which can even be used as parameters to other methods or classes without that those classes need to know where the settings come from, as long as the contract is respected. 这些接口可以提供特定于主题的设置,这些设置甚至可以用作其他方法或类的参数,而只要遵守合同,这些类就无需知道设置的来源。

public sealed class Settings : IUserStettings, IOSettings
{
    static readonly Settings instance = new Settings();

    static Settings(){ }

    Settings(){ }

    public static Settings Instance
    {
        get { return instance; }
    }

    //-- interface implementation

    public string UserName
    {
        get { throw new NotImplementedException(); }
    }

    // ---etc...

     public string filename
    {
        get { throw new NotImplementedException(); }
    }

    //-- interface implementation
}


public interface IOSettings
{
    string disk {get;}
    string path { get; }
    string filename { get; }
}

public interface IUserStettings
{
    string UserName { get; }
    string Password { get; }
}

And this can be used in a simple manner as as: 并且可以按以下简单方式使用:

    IOSettings iosettings = Settings.Instance as IOSettings;

    if(iosettings!=null){
        Filereader.ReadData(IOSettings iosettings);
    }

or 要么

    IUserSettings usersettings = Settings.Instance as IUserSettings;

    if(usersettings!=null){
        UserManager.Login(IUserSettings usersettings);
    }

My thoughts 我的想法

1- Static Classes are used when there is no reason to have instances like in .net Framework Math Class. 1-当没有理由像.net Framework Math Class这样的实例时,将使用静态类。

Math class is netural to be static becuase there is no good reason to have a object of this class and then maintain the object. 数学类是静态的,因为没有充分的理由拥有这个类的对象然后维护该对象。

2- Singleton pattern can be confused with static class concept but in singleton you get a whole object created in memory. 2-单例模式可以与静态类概念混淆,但是在单例中,您会在内存中创建一个完整的对象。

so it depends in the end whats your requirement. 因此最终取决于您的要求。

Seen from at OO/testability point of view neither is a particular good solution. 从OO /可测试性的角度来看,这都不是一个特别好的解决方案。 Search SO for singleton to see arguments. 在SO中搜索单例以查看参数。 That said don't use static for state only use if you need/want to write procedural code as is done with Math 也就是说,不要仅将静态用于状态,仅当您需要/想要像Math一样编写过程代码时才使用静态

I think people tend to use singleton classes when they actually want a static class but need to be sure that the class is initiated with some value. 我认为人们在实际需要静态类时倾向于使用单例类,但需要确保该类以某个值启动。 But really they should both be avoided if you can. 但实际上,如果可以的话,都应避免同时使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM