2015-01-16 137 views
0

我使用單例模式來構建一個類,但它確實接收另一個單例作爲參數,它不工作,是真的有可能嗎?單身進入一個無限循環

這是整個項目:https://github.com/raphaelbgr/SwingSocketClient/tree/master/src

package clientmain; 

import gui.janelas.JanelaMain; 

public class ClientMain { 

    public static int port    = 0; 

    public static Thread receiver   = null; 

    public static String ip     = null;  
    public static String your_name  = null;  

    public static void main(String[] args) {  
     JanelaMain jam = JanelaMain.getInstance(); //JanelaMain extends JFrame 
     jam.setLocationByPlatform(false); 
    } 

這是它的單塊。

//SINGLETON PATTERN BLOCK 
private static JanelaMain jam; 
public static JanelaMain getInstance() { 
    if (JanelaMain.jam == null) { 
     jam = new JanelaMain(JanelaSelectServer.getInstance()); //Another one passed by parameter 
    } 
    return jam; 
} 

另一類單件塊:

//SINGLETON PATTERN BLOCK 
private JanelaSelectServer() {} //JanelaSelectServer extends another JFrame 
private static JanelaSelectServer jsv; 
public static JanelaSelectServer getInstance() { 
    if (jsv == null) { 
     jsv = new JanelaSelectServer(); 
    } 
    return jsv; 
} 

媽發生(A環路)

s.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144) 
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13) 
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34) 
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37) 

感謝。

+0

你是否有意使'if(JanelaMain.jam == null){'等於'if(jam == null){' –

+1

哇,單身人士取決於另一個單身人士?你可能在那裏發明了一種新的反模式......我建議我們稱它爲Twingleton模式*,因爲雙胞胎也相互依賴。 – fredoverflow

回答

2

問題不在於這些類,而在於ConnectionLog類。您正試圖在JanelaMain中創建一個實例,並且ConnectionLog試圖創建JanelaMain的一個實例。

@SuppressWarnings("serial") 
public class JanelaMain extends JFrame { 

private FlowLayout layout       = new FlowLayout(); 
private JTextField jtxt_send      = null; 

//CUSTOM SWING COMPONENTS 
private ConnectionLog cn_log    = ConnectionLog.getInstance(); 
private TextLog msg_list     = TextLog.getInstance(); 


@SuppressWarnings("serial") 
public class ConnectionLog extends JTextField { 

private JanelaMain jam = JanelaMain.getInstance(); 
+0

你殺了它,循環是由構造函數調用產生的,然而,它不應該認爲靜態類的值像null,並且循環應該停止。 – raphaelbgr

3

你的代碼看起來有點奇怪,爲什麼給另一個singelton?你可以在你需要的地方使用singelton類,而不需要把它交給另一個類。