在Java中实现单例模式 [英] Implementing the singleton pattern in Java

查看:118
本文介绍了在Java中实现单例模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供单身人士模式的例子并解释为什么他们是必要的吗?

解决方案

在走单身路线之前,重新考虑。你真的需要一个单身人士吗?如果您在需要实现单例时要求场景,那是因为对它们的需求并没有真正表达出来。你最好不要在你的代码库中引入单例,因为遵循设计模式会感觉很酷。



清洁代码会谈 - 全球州和单身人士



一次不够



Performant Singletons



然而,什么是真的值得一提的是依赖注入



现在如果你真的想用Java实现单例,我会推荐Joshua Bloch的有效Java实现它们的方式:

  public class Singleton 
{
public static Singleton getInstance(){
return SingletonHolder.instance;
}

private Singleton(){}

private static final class SingletonHolder {
static final Singleton instance = new Singleton();
}
}

JLS 保证JVM不会初始化实例,直到有人调用 getInstance();



最后注释,双重检查锁定模式在Java中被破坏,直到Java 5 Java 5内存模型使DCL模式线程安全,但它使得它比 SingletonHolder 类方法,而原始意图是性能优化



编辑:正如@Luno指出的那样,自本书第二版以来,首选方式是:


从版本1.5开始,第三种方法是实现单例。只需使用一个元素创建一个枚举类型:




  //枚举单例 - 首选方法
public enum Elvis {
INSTANCE;

public void leaveTheBuilding(){...}
}



< blockquote>

这种方法在功能上等同于公共字段方法,除了它更简洁,免费提供序列化机制,并提供防止多实例化的铁定保证,即使面对复杂的序列化或反射攻击。虽然这种方法尚未被广泛采用,但单元素枚举类型是实现单例的最佳方式。



Can anyone provide an example of a singleton pattern and explain why they are necessary?

解决方案

Before going the singleton route, reconsider. Do you really need a singleton? If you're asking for scenarios when you need to implement singletons, it's because the need for them didn't really express. You're better off not introducing singletons in your code base just because it feels cool to follow design patterns.

Clean Code Talks - Global State and Singletons

Once Is Not Enough

Performant Singletons

However, what's really worth knowing is Dependency Injection.

Now if you really want to implement singletons in Java, I would recommend Joshua Bloch's "Effective Java" way of implementing them:

public class Singleton
{
  public static Singleton getInstance() {
    return SingletonHolder.instance;
  }

  private Singleton() {}

  private static final class SingletonHolder {
    static final Singleton instance = new Singleton();    
  }
}

The JLS guarantees the JVM will not initialize instance until someone calls getInstance();

Final note, the Double Checked Locking Pattern is broken in Java up to Java 5. The Java 5 memory model makes the DCL pattern thread safe but it makes it slower than the SingletonHolder class method while the original intent was performance optimization.

EDIT: As @Luno pointed out, since the second edition of the book, the preferred way is:

As of release 1.5, there is a third approach to implementing singletons. Simply make an enum type with one element:

// Enum singleton - the preferred approach
public enum Elvis {
    INSTANCE;

    public void leaveTheBuilding() { ... }
}

This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

这篇关于在Java中实现单例模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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