[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Another design question...

Van Dyk, Joe

11/12/2004 7:49:00 PM

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


3 Answers

Zach Dennis

11/12/2004 8:52:00 PM

0

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



Van Dyk, Joe

11/12/2004 9:59:00 PM

0

Zach Dennis wrote:
> 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

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


Zach Dennis

11/12/2004 10:44:00 PM

0

Joe Van Dyk wrote:

>
>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