单例类和静态类之间的区别? [英] Difference between singleton class and static class?

查看:119
本文介绍了单例类和静态类之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

静态类和单例模式之间的区别?

单例模式和Java中的静态类有什么区别?

HI
我不明白得到单例类和静态类有什么区别?
有人可以用例子详细说明吗?

HI I am not clearly getting What’s the difference between a singleton class and a static class? Can anybody elaborate this with example?

推荐答案

Singleton类:Singleton类是类每个类加载器只能存在一个实例。

Singleton Class: Singleton Class is class of which only single instance can exists per classloader.

静态/辅助类(仅包含静态字段/方法的类):没有此实例类存在。只有字段和方法可以作为常量或辅助方法直接访问。

Static/Helper Class (Class with only static fields/methods): No instance of this class exists. Only fields and methods can be directly accessed as constants or helper methods.

以下是从博客 Java中的静态类很好地描述了它。该博客还有解释相同的示例:

Following is referenced from this blog "Static classes in Java" describes it nicely. The blog also has examples for explaining same:

Singleton 示例:

public class ClassicSingleton {
    private static ClassicSingleton instance = null;

    private ClassicSingleton() {
        // Exists only to defeat instantiation.
    }

    public static ClassicSingleton getInstance() {
        if (instance == null) {
            instance = new ClassicSingleton();
        }
        return instance;
    }
}

静态类示例:

/**
 * A helper class with useful static utility functions.
 */
public final class ActionHelper {

    /**
     * private constructor to stop people instantiating it.
     */
    private ActionHelper() {
        // this is never run
    }

    /**
     * prints hello world and then the users name
     * 
     * @param users
     *            name
     */
    public static void printHelloWorld(final String name) {
        System.out.println("Hello World its " + name);
    }

}

那么两者之间有什么区别示例以及为什么我认为第二种解决方案对于您不想要或不需要实例化的类更好。首先,如果要创建一个类的实例,Singleton模式非常有用。对于我的助手类,我们并不想真正实例化该类的任何副本。你不应该使用Singleton类的原因是因为对于这个助手类我们不使用任何变量。如果单例类包含一组我们只需要一组变量并且方法使用这些变量但是在我们的帮助器类中我们不使用除传入的变量之外的任何变量(我们制作最终变量),这将是有用的。 。出于这个原因,我不相信我们想要一个单例实例,因为我们不想要任何变量,我们不希望任何人实例化这个类。因此,如果您不希望任何人实例化该类,通常如果您有某种类型的helper / utils类,那么我使用我称之为静态类的类,一个带有私有构造函数的类,并且只包含静态方法而没有任何类任何变量。

So what's the difference between the two examples and why do I think the second solution is better for a class you don't want or need to instantiate. Firstly the Singleton pattern is very useful if you want to create one instance of a class. For my helper class we don't really want to instantiate any copy's of the class. The reason why you shouldn't use a Singleton class is because for this helper class we don't use any variables. The singleton class would be useful if it contained a set of variables that we wanted only one set of and the methods used those variables but in our helper class we don't use any variables apart from the ones passed in (which we make final). For this reason I don't believe we want a singleton Instance because we do not want any variables and we don't want anyone instantianting this class. So if you don't want anyone instantiating the class, which is normally if you have some kind of helper/utils class then I use the what I call the static class, a class with a private constructor and only consists of Static methods without any any variables.

我的回答也引用了相同的答案这里

Same answer is also referenced from my answer here

这篇关于单例类和静态类之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆