Zach Dennis
11/12/2004 8:52:00 PM
Joe Van Dyk wrote:
>If you remember my previous post titled "automatically call function on
>attribute set", you'll remember that I have an airplane class and a
>airplane_drawing class, which represents the airplane when drawn on the
>screen.
>
>I'm using GTK for this, by the way.
>
>Say the user right clicks on the airplane_drawing object. I want a menu to
>popup on the screen with options that when selected will call methods on the
>airplane object.
>
>Information on the airplanes is also listed in table form, which reports
>each airplane's position, velocity, etc. When I right click on the row that
>represents the airplane, I'd like the same popup menu (for now, perhaps it
>will be different in the future) to pop up with options that when selected
>will call a method on the airplane object.
>
>Question: Since the popup menu has pretty common functionality between the
>graphical airplane display and the table airplane display, what would be a
>good way to generalize this? Any patterns that would be useful here?
>
>
>
For now you say you want the same pop-up menu (meaning same
functionality?). If this is the case, make a Singleton class that
represents your popup window and it's functionality.
If your airplane_drawing and your table will utilize *most* of the same
functionality then still use a Singleton, and allow it to be passed
parameters which act as switches. These switches could depict what
functionality shows up.
Conceptually speaking maybe something like:
#singleton
class AirplanePopupMenu
@instance
#constants
AP_VELOCITY = 1
AP_LOCATION = 2
AP_SIZE = 3
AP_FUEL = 4
AP_WEIGHT = 5
#etc...
def get_instance
@instance
end
def show_menu( *args )
#Loop through arrat args which should correspond to the AP_xxx
constants we specified
# above. Depending on the switch then build the rest of your
popup menu before displaying it
# This way users of the AirpanPopupMenu could only utilize the
functionality they want
end
end
Hope I didn't muddy the waters...
Zach