The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Singelton pattern in GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Singelton pattern in GUI" Watch "Singelton pattern in GUI" New topic

Singelton pattern in GUI

Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 45
Hi there,

After selecting mode my program displays a frame (extends JFrame) with the menu bar. When user selects any option (eg Search by Name). control in Program goes to another file(without extends JFrame) that has different gui components(including JPanel(s) but not JFrame) and other relevant code. Now I remove any previous existing panel and display this new panel(s) inthe old frame with same menu bar. ofcourse used Singelton for this.

I read in some forums on net that singelton in GUI does not work good in some operating systems when have many clients. Another says Singelton pattern is anti pattern.

Can you, my fellows, please give your opinion about this or any better design.

Thanks! in Advance
Best Regards!
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69

singleton is nothing else by a unique object instance. Each time you will launch a new Client the VM will create a new instance for the new launch, this not going to be shared between the two clients. As i understood you have a main frame and you remove and place panels according to the user's action. Speaking for design point of view it would have been better if you have used a Cardlayout and according to the user's action display the apropriate card.

Singleton will behave just fine like other clasess as long as you manipulate its content thread safe, all operations done on Swing code to be performend by the event dispatch thread.

Hope i helped!

SCJP 6.0 , SBCD (Preparing..),
MSc Enterprise Systems Development, BSc Computer Science
Roel De Nijs

Joined: Jul 19, 2004
Posts: 8389

Hi Tahir,

I agree with Nicolas on this one: if you decide to switch panels in your main window, you should use the CardLayout, because that layout is designed to do what you want. If you use your own custom system (singleton pattern or not), it conflicts with this principle (from instructions.html):
Use of functionality provided by the core Java classes will be preferred to your own implementation of that functionality, unless there is a specific advantage to providing your own implementation.

Kind regards,

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 45
Thanks you very much for this useful information.
subject: Singelton pattern in GUI
It's not a secret anymore!