I can't think of many reasons (or any at all) in which multiple independent user interfaces should be instantiated in a desktop application. Is it good practice to implement the UI as a singleton? Are there are advantages or disadvantages to this approach?
views:
296answers:
5uh...i open multiple copies of Internet Explorer, Word, and Excel all the time!
EDIT: I also have several emails open at once in Eudora
I see no good reason to restrict a user-interface to a singleton...
What advantage does a singleton give you? Are you aware of the potential problems of this pattern (e.g. multithreading issues with all obvious implementations)?
In practice, if there's no compelling reason to use it, you might want to avoid using the Singleton. For the UI, there isn't actually a compelling reason even if it doesn't get instantiated multiple times.
A singleton is a global variable. You making it a global encourages you to reference your controls all over your other classes (Imagine your data abstraction layer grabbing a name/addresss pair from the Database, and just for convenience, updating your name/address textbox right there) The resulting code is brittle and the dependencies become hard to resolve. Much easier just to have responsibility limited to a certain subset of classes.
I can think of a couple:
1) Document interfaces. If your application deals with documents, it's better to let each one have its own window. It makes it easier for the user to arrange their workspace and switch between documents using the OS's app switching capabilities. A lot of MDI applications prevent this by corraling multiple documents into the same window, which limits usability of screen real estate in a multiple monitor scenario.
2) Views. It's often the case that several different views on the data behind your UI cannot be easily combined in a single UI instance. You can't always predict what combinations of data your user will want to see at the same time, so it's better to give them the flexibility to instantiate multiple UIs.
As an aside, it's worth noting that many people consider Singleton an anti-pattern.
You use a singleton for cases where it's an error to have more than one instance of a type of object, not because you can't think of any good reason to have more than one instance.