Singleton -> Constructor needs a parameter :(
Hello,
i have a Problem with the Singleton Pattern.
Following situation:
I have a class (Commander) which starts all the dialogs of my Swing Application. For this it starts first the BPs and they start the dialogs.
The BPs need my mainprocess class in the construtor to set the its parent. So, my commander-class needs to keep an instance of the mainprocess-class as a member.
At the moment the constructor of my commander class gets the mainprocess instance, but i want to make the commander singleton.
When i do that i am not able to use the constructor to publish the mainprocess class. I dont want to use the getInstance-method to give my the commander, the mainprocess.
It seems impossible to make the commander singleton as long as it needs the mainprocess for each other operation. Is that right or do you have a solution for my problem?
thanks and regards
Toni
[929 byte] By [
toniG.a] at [2007-10-2 9:59:09]

The best i see is to add a init method that receives your "mainprocess class", beeing the execution of this method executed as the mainprocess starts.
In all methods of the "commander class" that might require the "mainprocess class" you may throw a IllegalStateException if the "mainprocess class" is not defined (what should not happen if the initialization is processed when the "mainprocess class" starts).
Another way, is to make your "commander class" start the "mainprocess class".
Hope it helps,
Daniel Campelo
Does something like this work?
public final class Commander {
private static Commander instance;
private MainProcess main;
private Commander(MainProcess p) {
main = p;
}
public static synchronized Commander createInstance(MainProcess p) {
if (instance == null) {
instance = new Commander(p);
} else {
throw new UnsupportedOperationException("Instance already created!");
}
}
public static synchronized Commander getInstance() {
if (instance != null) {
return instance;
} else {
throw new UnsupportedOperationException("Instance not created!");
}
}
}