Newsgroup: it.comp.java
Titolo: No more than one instanc of a class

  Hi.
Can somebody tell me if there is a way that you can keep more than one instance
of a class from being created in a program?  Currently, I have a program that
creates a frame along with several components placed on it.  This frame is
triggered by the user selecting an option from a menu.  If that frame manages
to get hidden behind the main frame that is created when then program first
starts up, a user could go up to the menu and select that option again which
would cause a new instance of that class to be created.  Obviously this would
not be a good situation.  I was hoping that there was some way that if the user
did select that option for a second time before the first instance of that
class had been closed, the program would just set focus to the object that had
already been created.
If somebody could help me out with this, I would be truly grateful.
Thanks.
Sincerely,
Steve Rulison
PS.  If you wouldn't mind, I would like to get your responses to my question in
an email.
Thanks again.

 
Per evitare la comparsa di una seconda Frame puoi provare qualcosa del 
genere:
class MyFrame extends JFrame
{
	OptionFrame iOptionFrame;
	void showOptionFrame()
	{
		if(iOptionFrame==null)
		{
			iOptionFrame=new OptionFrame(...);
			iOptionFrame.setVisible(true);
			iOptionFrame.addWindowListener(new MyListener());
		}
		else
			iOptionFrame.requestFocus();
	}
	class MyListener extends WindowAdapter
	{
		void windowClosing(WindowEvent e)
		{MyFrame.this.iOptionFrame=null;}
	}
 
}
class OptionFrame extends JFrame
{
	OptionFrame(....)
	{....}
}
E'molto semplice ma funziona.
Se invece hai bisogno di essere sicuro che un oggetto non possa essere 
istanziato penso che dovresti dichiarare il costruttore privato e 
memorizzare un riferimento all'unica istanza come membro statico della 
classe. Tipo:
class MyServer
{
	static MyServer iServer;
	private MyServer (...)
	{...}
	public static MyServer getServer()
	{
		if(iServer==null)
			 iServer = new MyServer(..);
		return iServer;
	}
}
Ciao



of a class from being created in a program?  Currently, I have a program
that
This newsgroup is reserved for posts in Italian (IT.etc).
Questo gruppo e` riservato a post in italiano (IT.ecc).
Re your problem: make constructors private and supply a public
static function newFoo, called by class clients when they want
the class to be created:
    class Foo {
        private Foo() {};
        private static theFoo;
        public static Foo newFoo() {
            if(theFoo == null) theFoo = new Foo;
            return theFoo;
        }
    }
In this simple example, once a Foo has been created, any client
asking for a "new" one will in fact get a reference to that Foo.
Approccio generale di soluzione (v. esempio teste` portato):
fare i costruttori privati, fornire una funzione static public,
unica che puo` creare oggetti della classe, che va chiamata
dai clienti quando desiderano farne "creare" -- nell'esempio
teste` visto, una volta creato il primo Foo, resta anche l'unico,
perche` qualsiasi cliente ne chieda uno "nuovo" ottiene invece
un riferimento a quel primo-e-unico.
This is the simplest and most basic form of the "Singleton"
Design Pattern (Gamma, Vlissides, etc, "Design Patterns").
Questa e` la forma piu` semplice e fondamentale della
design pattern "Singleton".
Alex