1

我想使用以下模式在java中创建一个单例

public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }

        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
                public static final Singleton INSTANCE = new Singleton();
        }

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

但是当我要调用的私有构造函数是

 private Singleton(Object stuff) {... }

我如何传递stuffINSTANCE = new Singleton()? 如在INSTANCE = new Singleton(stuff);

重写上面的代码片段:

public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton(Object stuff) { ... }

        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
                public static final Singleton INSTANCE = new Singleton();
        }

        public static Singleton getInstance(Object stuff) {
                return SingletonHolder.INSTANCE;//where is my stuff passed in?
        }
}

编辑:

对于那些声称这种模式不是线程安全的人,请在此处阅读:http ://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh 。

我传入的对象是 android 应用程序上下文。

4

4 回答 4

5

如果你真的想要一个单例,应该只有一个实例(呃!)。如果您向其中添加参数,getInstance您可能希望返回的实例不同(否则不需要参数),这违背了目的。

如果您的目标是在创建唯一实例时添加一些配置,最简单的方法是在实例化时让您的单例查询配置信息:

public static final Singleton INSTANCE = new Singleton(getConfiguration());

其中 getConfiguration 返回所需的内容(例如,通过读取文件或转发其他变量)。


通常的免责声明:单身人士是邪恶的
附加资源:编写可测试代码的 Google 指南(以防您第一次不相信)。

于 2013-07-24T16:23:05.090 回答
0

您可能想阅读

带参数的单例不是单例

第一个答案争论为什么带有参数<<的>>单例不是单例,并且不接近单例。

于 2013-07-24T16:24:39.640 回答
0
public class Singleton {
        private static Singleton singleton;

        // Private constructor prevents instantiation from other classes
        private Singleton() { }

        public void addStuff(Object stuff){}    

        public static Singleton getInstance() {
                 if(singleton == null) singleton = new Singleton()
                 return singleton;
        }
}

并将其用作:

 Singleton s = Singleton.getInstance();
 s.addStuff(stuff);

或替代方案

public class Singleton {
        private static Singleton singleton;

        // Private constructor prevents instantiation from other classes
        private Singleton() { }

        public static void redefine(Object stuff){

             singleton = new Singleton(stuff) // choose constructor based on parameters
        }


        public static Singleton getInstance() {
                 return singleton;
        }
}
于 2013-07-24T16:21:59.033 回答
-1

为什么不摆脱 SingletonHolder 使用工厂模式。当尝试调用 getInstance 两次但使用不同的“东西”时,您将不得不决定要做什么。

public class Singleton {

    private static Singleton singleton
    private final Object stuff;

    private Singleton(Object stuff) {
        this.stuff = stuff;
    }

    public static synchronized Singleton getInstance(Object stuff) {
         if (singleton == null) {
             singleton = new Singleton(stuff);
             return singleton;
         }

         return singleton; // or throw error because trying to re-init
   }
}
于 2013-07-24T16:23:17.093 回答