Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3159

Re: Singletons and Swing

From "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this>
Subject Re: Singletons and Swing
Message-ID <op.t6jc4ohv8jd0ej@petes-computer.local> (permalink)
Newsgroups comp.lang.java.gui
References <74523a1e-4294-4626-a555-7b1fe5a1c4b3@s19g2000prg.googlegroups.com>
Date 2011-04-27 15:43 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.programmer
On Thu, 14 Feb 2008 14:20:03 -0800, Jason Cavett <jason.cavett@gmail.com>  
wrote:

> [...]
> Your suggestions seems to be a good one, except one thing.  If the
> user closes a project, how can I make sure that the InsertMenu
> associated with that project is destroyed?

Define "project".  But more generally, I'd say that'd be a function of the  
rest of your code.  I presume that normally, you'd just rely on the  
destruction of the frame and contained objects associated with the  
"project" to release the menu.

In this case, you'd also want to remove your menu instance from the  
collection the factory is managing, right?

Depending on how you're dealing with the destruction of the project, I  
suppose there are a variety of ways you could react to this event.  But  
one obvious answer seems to be that you could add a WindowListener to the  
frame so that you are called when it's closing or closed.  At that point,  
you'd destroy whatever resources are associated with the project,  
including the factory's reference to the menu (the menu(s)  
itself(themselves) used by the frame would be destroyed with the  
destruction of the frame, presumably...and I use the term "destroy"  
loosely since I'm not really sure at what point things get disposed versus  
just not being referenced any longer...I assume that disposing the frame  
disposes all of the contents, but like I said, I'm new to Java :) ).

I assume that in this case by "InsertMenu" you mean the actual Java menu,  
and not the factory itself (and of course that assumes we're talking about  
going with a factory implementation rather than a singleton, as a  
work-around to the "no multiple parents" issue).

> In reference to your last question - I am lazily instantiating the
> menu per:  
> http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh

I see.  Again, I doubt it matters but...do you really need this singleton  
to be thread-safe?  I like lazy instantiation, but I think it's a bit  
clearer to write the code explicitly rather than relying on the specific  
behavior of the language.  If you don't need to synchronize access to the  
singleton, the simple "if null then instantiate" approach won't clutter up  
the code very much at all.  Maybe that's just me though.  :)

Pete

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
  Re: Singletons and Swing "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
    Re: Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
      Re: Singletons and Swing "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
      Re: Singletons and Swing "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
  Re: Singletons and Swing "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
    Re: Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000

csiph-web