我使用單例模式來構建一個類,但它確實接收另一個單例作爲參數,它不工作,是真的有可能嗎?單身進入一個無限循環
這是整個項目: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)
感謝。
你是否有意使'if(JanelaMain.jam == null){'等於'if(jam == null){' –
哇,單身人士取決於另一個單身人士?你可能在那裏發明了一種新的反模式......我建議我們稱它爲Twingleton模式*,因爲雙胞胎也相互依賴。 – fredoverflow