Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singelton pattern in GUI

 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tahir,

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!
 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Roel
 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you very much for this useful information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic