简体   繁体   English

如果不是为了延迟初始化,那么使用方法而不是静态类成员来构建单例是否有任何优势?

[英]If not for lazy initialisation, is there any advantage of building singleton using method rather than static class member?

Very often I see singleton built in this way: 我经常看到单例以这种方式构建:

public static MyClass instance() {
    if (singleton == null) {
        singleton = new MyClass();
    }
    return singleton;
}

If not for the lazy initialization effect, does the approach have any advantage over simply declaring a static instance like this? 如果不是出于延迟初始化的效果,那么这种方法是否比简单地声明一个静态实例具有任何优势?

public final static MyClass singleton = new MyClass();

No, in fact the other approach, ie: 不,实际上是另一种方法,即:

public final static MyClass singleton = new MyClass();

might be better as, if you you have 2 threads calling the instance method at the same time you could get a race condition. 可能会更好,因为如果您有2个线程同时调用实例方法,则可能会获得竞争条件。

This is how Java in Practice says to do singletons: 这是Java在实践中说做单例的方式:

  private final static MyClass _instance = new MyClass();

  public static MyClass getInstance() {
    return _instance;
  }

  private MyClass() {}

Update Since @jon-skeet mentioned it there is really good discussion of Singletons in the book Effective Java by Joshua Block. 更新由于@ jon-skeet提到了它,Joshua Block撰写的有效Java一书中对Singleton的讨论非常好。 One thing he points out is that if you want your Singleton to be serializable you can't just implement Serializable. 他指出的一件事是,如果您希望Singleton可序列化,则不能仅仅实现Serializable。 You need to override the readResolve method as well. 您还需要重写readResolve方法。 Use the above approach makes this easy: 使用上述方法使此操作变得容易:

private Object readResolve() throws ObjectStreamException {
  return _instance;
}

Update 2 : Checkout this excellent discussion on Singletons linked to by @mardavi: http://en.wikipedia.org/wiki/Singleton_pattern#Initialization_On_Demand_Holder_Idiom 更新2 :通过@mardavi链接查看关于Singletons的精彩讨论: http : //en.wikipedia.org/wiki/Singleton_pattern#Initialization_On_Demand_Holder_Idiom

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

相关问题 为什么要在静态锁定成员而不是类上进行同步? - Why synchronize on a static lock member rather than on a class? 在不使用静态方法的情况下制作类单例 - Making a class singleton without using static method 静态方法属于类而不是类的对象 - A static method belongs to the class rather than object of a class 使用基于对象的run()方法而不是静态main是否有任何优势? - Is there any advantage to using a object based run() method instead of the static main? 与类/对象相比,使用静态函数/变量有什么好处吗? - Is there ever any advantage to using static functions / variables versus a class / objects? 在实例上调用方法,而不是在不能使用静态类型的类上调用 - Calling the method on an instance rather than the class not working with static type Java - 使用JBOSS / WildFly邮件服务而不是Apache Commons Email发送邮件有什么好处吗? - Java - is there any advantage sending mail using JBOSS/WildFly mail service rather than Apache Commons Email? 有或没有持有者的单例 = 懒惰 vs 急切初始化? - Singleton with or without holder = lazy vs eager initialisation? java类成员初始化 - java class member initialisation 使用checkstyle而不是使用Eclipse内置代码格式化程序的优势? - Advantage of using checkstyle rather than using Eclipse built in code formatter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM