• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern on WindowListener Class

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that implements WindowListener that I use to track all my WindowEvents. Is it a good idea or bad idea to use a Singleton pattern on the WindowListener? So that each object just uses the same instance instead of creating new Objects in every class I need.
thanks.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just create new objects. but if the event gives you information on who fired it, and who its fining on, then you should be ok with the singleton. How will you unregister the listener if you choose too?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, when using the singleton pattern I just have a getInstance() method. So say I have a Class that extends JFrame. Somewhere at the beginning of the constuctor I will do
windowListen = myWindowListener.getInstance();
this.addWindowListener(windowListen);
and then if I need to remove the listener
this.removeWindowListener(windowListen);
I have been testing this over the past day or so and haven't ran into any problems yet.
Still looking for opinions though.
CL - Thanks for yours
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!