Re: How do I retrieve the 'type' of an object



* Daniel Kasak <dkasak nusconsulting com au> [2004-06-07 00:33]:
I'm looping through a set of objects, and I want to get the
type of object, ( eg GtkEntry ) and then decide what to do
based on the type.  How do I do that?

The *correct* way would be not to.

You want to dispatch to the right code/function depending on the
type of object. Notice something? That's what a method call is.

The correct way would be to add this "what to do" as a method to
each of the classes involved. Then you do something like

    foreach my $field (@{$self->{fieldlist}}) {
        $self->{form}->get_widget($field)->do_foo_bar();
    }

and that method call it figures out.

This is the central idea of object orientation: instead of
explicitly checking for types, you let the method call dispatch
to the right place implicitly.

See http://c2.com/cgi/wiki?ReplaceConditionalWithPolymorphism

I'm not sure how much work would be involved for such an approach
with Gtk2-Perl though, since I've not subclassed/extended
anything yet, only used what was already there.

-- 
Regards,
Aristotle
 
"If you can't laugh at yourself, you don't take life seriously enough."



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]