使用静态内部类创建java单例

时间:2013-07-24 16:12:17

标签: java android singleton

我想使用以下模式在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) {... }

如何将stuff传递给INSTANCE = 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 个答案:

答案 0 :(得分:5)

如果你真的想要一个单身人士,那么它应该只有一个实例(呃!)。如果向getInstance添加参数,您可能希望返回的实例不同(否则不需要参数),这会使目的失效。

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

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

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


通常免责声明:Singletons are evil
附加资源:Google guide to writing testable code(如果您第一次不相信)。

答案 1 :(得分: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;
        }
}

答案 2 :(得分:0)

您可能想要阅读

a singleton with parameters is not a singleton

第一个答案是为什么a>>单身的参数<<不是单身人士,也不是靠近单身人士。

答案 3 :(得分:-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
   }
}