June 19,200 8
An interesting, although generally basic, problem with Windows Forms is how to implement the Singleton pattern on the forms. What do I mean? Well, consider this...
You're writing a Windows Forms Application and you have a bunch of forms, such as a preference window, an about window and so on. you 'd like these forms to be modeless, yet you don't want the user to be able to create more than one of them.
It's a common problem, but there's no built in way to handle it. moreover, I 've yet to see an elegant solution to it. the method explained below, which is as elegant as it's going to get, creates a pseudo-factory class to manage the problem for you. read on to find out more.
If a form were just any old object, you cocould implement the Singleton pattern in the said class:
View plaincopy to clipboardprint?
- Class somesingletonclass {
-
- Protected somesingletonclass (){
-
- }
-
-
- Private Static somesingletonclass minstance = NULL;
-
-
- Public static somesingletonclass instance {
-
- Get {
-
- If (minstance = NULL) minstance = new somesingletonclass ();
- Return minstance;
-
- }
-
- }
-
- }
Of course, this isn' t thread safe-there are much better ways to do this in the general case. see here for a good explanation. in the case of forms, however, we don't need to be thread safe. windows Forms are explicitly un-threadsafe themselves. they shocould (in most cases) only ever be initialized/used from a single thread. this constraint applies here too.
Now, this won't work for Windows Forms. How come? Well, once the Windows form has been closed (with close () or otherwise) It, generally, gets disposed. we all know you can't use a disposed form again. you'll get a crash when trying to access the form again. so, what's the work around?
Basically, we need to handleFormclosedEvent, so that we know the form has been closed. we can then remove the reference to the form, once its been closed, and start all over again next time it's needed. so we 'd end up with something like this:
View plaincopy to clipboardprint?
-
- Class somesingletonform: FORM {
-
- Protected somesingletonform (){
-
- }
-
-
- Private Static somesingletonform minstance = NULL;
-
-
- Public static somesingletonform instance {
- Get {
-
- If (minstance = NULL)
-
- {
-
- Minstance =New somesingletonclass ();
-
- Minstance. formclosed + =New formeventhandler (remover );
-
- }
- /* Cocould call minstance. Show ();*/
-
- Return minstance;
-
- }
-
- }
-
-
- Static void remover (Object sender, formclosedeventargs E)
-
- {
-
- Minstance. formclosed-=New formclosedeventhandler (remover );
-
- Minstance =NULL;
- }
-
- }
Now we're re getting closer. it seems unnecessary, however, to implement this on every single form that we want to be a singleton form: it's a lot of copying identical code, and it's prone to bugs.
So what can we do? We create a provider class which creates Singleton forms for us. sort of like a singleton factory with a few little tricks to ensure only valid forms are around. I 've seen code before for this on various blogs/message boards, but none of them are as complete as this one.
What's the difference? It's the use of generics that makes this implementation really handy. By using a generic method, we can return a form of the correct type: There's no need to worry about casting.
Another modification is the addition of a parameters to the getinstance method. although this is a little Dodge, and cocould break things if you aren't careful, I 've found it handy when used with a form whose constructor required extra arguments.
So here it is:
View plaincopy to clipboardprint?
-
- Class singletonformprovider
-
- {
-
- Static dictionary <type, form> mtypeformlookup = new dictionary <type, form> ();
-
-
- Static public t getinstance <t> (Form owner)
-
- Where T: Form
- {
-
- Return getinstance <t> (owner, null );
-
- }
-
-
- Static public t getinstance <t> (Form owner, Params object [] ARGs)
-
- Where T: Form
-
- {
-
- If (! Mtypeformlookup. containskey (typeof (t )))
-
- {
- Form F = (form) activator. createinstance (Typeof (t), argS );
-
- Mtypeformlookup. Add (Typeof (t), F );
-
- F. Owner = owner;
-
- F. formclosed + =New formclosedeventhandler (remover );
-
- }
-
- Return (t) mtypeformlookup [typeof (t)];
-
- }
-
- Static void remover (Object sender, formclosedeventargs E)
-
- {
-
- Form F = senderAs form;
-
- If (F = NULL) return;
-
-
- F. formclosed-=New formclosedeventhandler (remover );
-
- Mtypeformlookup. Remove (F. GetType ());
-
- }
-
- }
And to use it:
View plaincopy to clipboardprint?
- Aboutbox abox = singletonformprovider. getinstance <aboutbox> (this );
- Abox. Show ();
Well, there you have it. A handy generic Singleton form provider for C #.
Test