2013-07-24 46 views
1

創建Java單身我想用以下方式使用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應用程序上下文。

+1

你爲什麼認爲你的應用程序需要一個singleton? –

+4

您的描述可能是您不應該使用單身人士的標誌。特別是,如果你調用:Singleton.getInstance(object1); Singleton.getInstance(Object2的);'???你現在有兩個單例嗎?你是否改變了第一個例子?等等。 – assylias

+3

@LuiggiMendoza如果你想讓一個點使它。我不打算寫一篇關於爲什麼我需要一個單身人士的論文。 –

回答

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

我想過這個。但我想知道更具包容性的方法。 –

+0

我編輯了答案,也許這是你想要的? –

5

如果你真的想要一個單身,應該只有一個它的實例(廢話!)。如果向getInstance添加參數,則可能希望返回的實例不同(否則不需要參數),這會破壞目的。

如果你的目標是加入時的唯一實例被創建了一些配置,最簡單的方法是有你的配置信息單獨查詢時被實例化:

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

其中getConfiguration返回什麼需要(例如通過讀取文件或轉發其他變量)。


通常免責聲明:Singletons are evil
附加資源:Google guide to writing testable code(如果您第一次不服氣)。

-1

爲什麼不擺脫SingletonHolder使用工廠模式。嘗試調用getInstance兩次,但使用不同的'stuff'時,您將不得不決定要做什麼。

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 
    } 
} 
+0

'或拋出錯誤,因爲試圖重新初始化'這違背了單身人士的目的 –

+0

同意,但隨着問題的流動。我可能不會在單身人士中使用參數 –

+0

@assylias關於線程安全,我錯過了什麼?我無法發現它。 CHeers –