Another design question...

J

Joe Van Dyk

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?

Thanks,
Joe
 
Z

Zach Dennis

Joe said:
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
 
J

Joe Van Dyk

Zach said:
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

How would the AirplanePopupMenu object know what Airplane object's method to
use when the user selects an item from the popup menu?
 
Z

Zach Dennis

Joe said:
How would the AirplanePopupMenu object know what Airplane object's method to
use when the user selects an item from the popup menu?
You could couple Airplane and AirplanePopupMenu. This would allow
AirplanePopupMenu to know about the methods on the Airplane class. If
there are going to be more then one Airplane object at a time, then
allow for the AirplaneMenu to be passed an instance of Airplane. For
example:

class Airplane
def current_speed
return 290 #speed in mph?
end
end

class AirplanePopupMenu
AP_SPEED = 11

def get_menu( airplane, *args )
args.each{ |arg|
case arg
when AP_SPEED
#process the code to add a MenuItem for speed here to
your menu
menu_item = Menuitem.new
menu_item.set_label( airplane.speed )
@mymenu.add_menu_item( menu_item )
else
end
return @mymenu
end
end

I do not use GTK and I haven't used Ruby/Tk in a long while so I won't
even attempt to have the code make sense like that. I'll leave the
transitioning of code up to you.

Zach
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top