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

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

问题描述

可能的重复:
静态类和单例模式的区别?
有什么区别Java 中的单例模式和静态类之间的区别?

嗨我不是很清楚单例类和静态类有什么区别?有人可以举例说明这一点吗?

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 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:

单例示例:

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 类的原因是因为对于这个辅助类,我们不使用任何变量.如果单例类包含一组我们只想要一组变量并且方法使用这些变量,但在我们的助手类中,除了传入的变量(我们将其设为 final)之外,我们不使用任何变量,那么它会很有用.出于这个原因,我不相信我们想要一个单例实例,因为我们不想要任何变量,我们不希望任何人实例化这个类.因此,如果您不希望任何人实例化该类,通常情况下,如果您有某种帮助程序/实用程序类,那么我使用我所说的静态类,一个具有私有构造函数的类,仅由静态方法组成,没有任何任何变量.

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天全站免登陆