Winform Singleton pattern example

My blog has moved.
You can view this post at the following address:
http://www.osherove.com/blog/2004/1/23/winform-singleton-pattern-example.html
Published Friday, January 23, 2004 11:33 AM by RoyOsherove
Filed under:

Comments

Friday, January 23, 2004 6:57 AM by Johnny Hall

# re: Winform Singleton pattern example

What do you do if you want to have a singleton form, which isn't the main form? e.g. an Options dialog.
Friday, January 23, 2004 7:00 AM by Sudhakar

# re: Winform Singleton pattern example

Infact GOF's Singleton has a flaw in example given in C++ due to it's language limitations. A Singleton instance is suppose to give exactly one instance at anytime and should not be tampered for getting multiple instances.

check out this

http://www.sadeveloper.net/viewarticle.aspx?articleID=77
Friday, January 23, 2004 8:10 AM by SBC

# re: Winform Singleton pattern example

good posting. The article at 'sadeveloper' is a good one - makes a good point about the threading aspects. I am not sure about using the 'sealed', since GoF does specify (if I recall correctly) that one can make it extensible with subclassing.
Friday, January 23, 2004 8:11 AM by Roy Osherove

# re: Winform Singleton pattern example

Johnny: This should work for any form.
Friday, January 23, 2004 9:09 AM by Johnny Hall

# re: Winform Singleton pattern example

I'm with you, I had a play. Ignore my stupidity. I was assuming something from the line:

Application.Run(Form1.Instance)

So I just use OptionsForm.Instance from wherever I want.
Friday, January 23, 2004 9:25 AM by Stefano Demiliani

# re: Winform Singleton pattern example

Thanks to all... so what's the better way? I think Roy solutions could be good...
Friday, January 23, 2004 12:19 PM by Patrick Cauldwell

# re: Winform Singleton pattern example

Your second criteria can be a tricky one. I'd agree that in most implementations, what people mean by "Singleton" is one and only one object that never gets destroyed. However, in other systems, "Singleton" just means "at most one", and not necessarily that it won't be destroyed. .NET Remoting is one example. If you use the "Singleton" activation mode for a server activated component, you get "at most one" but not necessarily the same object, and sometimes that might be appropriate.

Sorry, I realize that's not really relevant to WinForms in particular, just a general comment.
Friday, January 23, 2004 12:22 PM by Stuart Laughlin

# re: Winform Singleton pattern example