我习惯于在突然间编写枚举式单例,有人审阅我的代码时要求我更改它,因为它不在项目的编码约定中.他问我有什么好处,我只是说写起来比较容易.
我只想知道使用它是否真的没有任何好处.我知道java的枚举可能没有为此目的实现,但即便如此,以这种方式使用它是错误的吗?
通常我使用第一个实现.几天前我发现了另一个.任何人都可以解释这两个实现之间的区别吗?第二个实现是线程安全的吗?在第二个例子中使用内部类有什么好处?
//--1st Impl
public class Singleton{
private static Singleton _INSTANCE;
private Singleton() {}
public static Singleton getInstance(){
if(_INSTANCE == null){
synchronized(Singleton.class){
if(_INSTANCE == null){
_INSTANCE = new Singleton();
}
}
}
return _INSTANCE;
}
}
//--2nd Impl
public class Singleton {
private Singleton() {}
private static class SingletonHolder {
private static final Singleton _INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder._INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud) 我在stackexchange中遇到了另一篇关于实现java单例的各种方法的文章.显示的方法之一是以下示例.它的投票率非常低.想知道为什么. 在Java中实现单例模式的有效方法是什么?
public class Singleton {
private static Singleton instance = null;
static {
instance = new Singleton();
// do some of your instantiation stuff here
}
private Singleton() {
if(instance!=null) {
throw new ErrorYouWant("Singleton double-instantiation, should never happen!");
}
}
public static getSingleton() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个GUI应用程序.在我的主类中,我有一个方法(称为createAndShow())来初始化我的所有GUI类.在每个GUI类中,我必须static initializer读取属性文件(资源包或配置文件).如果文件或条目丢失或值错误,我会捕获异常,然后MissingResourceException故意将a抛出到上一级.在我createAndShow()的主类方法中,我放了一个try-catch来捕获Exception.但不知何故,JVM拒绝到达那里.每当文件丢失时,MissingResourceException抛出该文件然后应用程序就会挂起.我希望该createAndShow()方法将捕获该异常并优雅地退出.静态初始化程序的异常抛出有什么特殊之处吗?
我正在使用XP和java 1.6.
我已经实现了Singleton类,如下所示:
public class Singleton {
private static Singleton instance = null;
private Singleton() {
}
private synchronized static void createInstance() {
instance = new Singletone();
}
public static Singleton getInstance() {
if(instance == null){
createInstance();
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
但我想知道它是否是单例的正确实现.多线程环境中是否有任何问题.
我正在研究单身人士,我开发了一个非常基本的单身人士课程.
public class SingletonObject {
private static SingletonObject ref;
private SingletonObject () //private constructor
{ }
public static synchronized SingletonObject getSingletonObject()
{
if (ref == null)
ref = new SingletonObject();
return ref;
}
public Object clone() throws CloneNotSupportedException
{throw new CloneNotSupportedException ();
}
}
Run Code Online (Sandbox Code Playgroud)
现在下面是我破解单身人士的一种方式..
public class CrackingSingleton {
public static void main(String[] args) throws ClassNotFoundException,
IllegalArgumentException, SecurityException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
//First statement retrieves the Constructor object for private constructor of SimpleSingleton class.
Constructor pvtConstructor = Class.forName("CrackingSingleton.SingletonObject").getDeclaredConstructors()[0];
//Since the …Run Code Online (Sandbox Code Playgroud) 我在这一行得到一个空指针异常:
private ArrayList<DrawableEntity> entitiesToDraw = Loader.instance().getDrawableEntities();
Run Code Online (Sandbox Code Playgroud)
Loader的构造函数显然没有问题:
public static Loader instance() {
if (instance == null) {
new Loader();
System.out.println("Loader ready");
}
return instance;
}
Run Code Online (Sandbox Code Playgroud)
因为我收到消息"Loader ready".我不明白,问题似乎是在调用getDrawableEntities()之前,但我什么都没看到,它不在getDrawableEntities()中.
下面的类是否实现了单例模式?我真的很困惑.
class Customer {
static private Map costumers = new HashMap();
private Customer() {
}
public static Customer getInstance(String name) {
Customer instance = (Customer) costumers.get(name);
if (instance == null) {
instance = new Customer();
costumers.put(name, instance);
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我环顾四周,但找不到关于this在私有字段中分配是 Java 中的反模式的明确答案。考虑以下基于单例模式的示例:
public class Foo {
private static Foo INSTANCE;
private Foo() {
INSTANCE = this;
}
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是,在声明时,
this没有完全初始化,所以它不安全,因为如果任何其他调用会使用该static字段,它可能会找到一个没有完全初始化的实例。
那是正确的吗?这是我们应该避免的事情吗?如果是,为什么?有没有办法确保这些调用是安全的(因为我们不在INSTANCE构造函数中进一步使用)?
考虑Singleton:
public final class MySingleton {
private static class Nested
{
private static final MySingleton INSTANCE = new MySingleton();
}
private MySingleton()
{
if (Nested.INSTANCE != null)
{
throw new IllegalStateException("Already instantiated");
}
}
public static MySingleton getInstance()
{
return Nested.INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有放任何锁,但为什么这是Singleton问题的线程安全解决方案?