wood burning stoves*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Best design - you pick Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Best design - you pick" Watch "Best design - you pick" New topic
Author

Best design - you pick

Christopher Brooks
Greenhorn

Joined: Sep 16, 2002
Posts: 16
Excuse me, I did originally post this question in the Swing forum but felt it was more appropriate here...
I am having trouble deciding how to go about controlling the objects in my application. I want to have a JDesktopPane which when selecting menu options will spawn internal frames. The internal frames should themselves be able to spawn other internal frames.
Anyway I came up with a partial solution. In my GUI_DesktopPane class I have menu items and then also the following methods:

I then have various classes like GUI_MemberStatus that extends JInternalFrame and implements ActionListener. Now what should I do about those "special" internal frames that need to spawn internal frames in the JDesktopPane as well?
Should I:
a) Make the createFrame method static, and use it from wherever
b) Pass a reference of GUI_DesktopPane into the "special" internal frames constructor. Create the new internal frame within a method of the "special" internal frame. Call the createFrame method using the new internal frame as a parameter.
c) Make the GUI_DesktopPane a singleton. Call getinstance().createFrame from wherever I want.
d) Go away, read a book on design patterns, pass my SCJP exam, get a job in IT and stop trying to be a part-time programmer


SCJP 1.2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I would go with b), because it results in the loosest coupling. Your internal frames shouldn't depend on there being only one DesktopPane.
BTW, I would advice against the way you implemented the actionPerformed method - it violates the Single Choice Principle. That is, every time you add another menu entry, you need to add it to several distinct places, which is a bad thing. I would rather use one ActionListener per menu entry.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Christopher Brooks
Greenhorn

Joined: Sep 16, 2002
Posts: 16
I am glad you said b) because that's what I was leaning towards. I am not sure I understand your comment about the event handling. Do you mean that I should implement anonymous inner classes, so that the text "menuItem1" can appear in the same place in the source code?

instead of:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, that's what I meant (though I don't think there is an ActionAdapter - you might want to use AbstractAction instead).
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you're into MVC stuff, your listeners will do no more than notify the controller of a user gesture, eg clicked a button or selected a menu. The controller will have the logic to decide what to do with that.
Also think about a menu item and a button that do the same thing. Do you want to create two listeners with identical code in them? Have any other ideas?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
If you're into MVC stuff, your listeners will do no more than notify the controller of a user gesture, eg clicked a button or selected a menu. The controller will have the logic to decide what to do with that.

Agreed.

Also think about a menu item and a button that do the same thing. Do you want to create two listeners with identical code in them? Have any other ideas?

Remember the listener (temporarily) and add it to both widgets? I am doing this regularly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best design - you pick