簡體   English   中英

使用靜態內部類創建java單例

[英]Creating java singleton with static inner class

我想使用以下模式在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應用程序上下文。

如果你真的想要一個單身,那么它應該只有一個實例(呃!)。 如果向getInstance添加一個參數,您可能希望返回的實例不同(否則不需要參數),這會使目的失敗。

如果您的目標是在創建唯一實例時添加一些配置,最簡單的方法是在實例化時對配置信息進行單例查詢:

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

其中getConfiguration返回所需內容(例如,通過讀取文件或轉發其他變量)。


通常的免責聲明: 單身人士是邪惡的
其他資源: Google編寫可測試代碼的指南 (如果您第一次不相信)。

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;
        }
}

你可能想讀

帶參數的單例不是單例

第一個答案論證了為什么帶有參數<<的單例不是單例,並且不接近單例。

為什么不擺脫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
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM