in reply to Re: Re: Tutorial: Introduction to Object-Oriented Programming
in thread Tutorial: Introduction to Object-Oriented Programming

Although this is not a disadvantage, rather a reason to do it like Abigail proposes: You _will_ break things (and have subtle bugs to find) if you don't stick to documented accessor/mutator methods. Especially when you write a base class for all your classes that organizes a registry of all members of all instances(based on caller()), you'll see that if you write accessor methods for the same member with different names in two related classes, you assign different members, which is great but you probably didn't expect it because it has become a habit to have it work differently.

  • Comment on Re: Re: Re: Tutorial: Introduction to Object-Oriented Programming

Replies are listed 'Best First'.
Re: Re: Re: Re: Tutorial: Introduction to Object-Oriented Programming
by BrowserUk (Pope) on Dec 12, 2002 at 20:40 UTC

    Sorry fruiture, could you explain your point a bit more.

    I have read your post 3 times and keep wanting to react to it, but I'm sure I'm missing the salient point.

    I get lost right around "...a registry of all members of all instances...". Don't all members of a given class have the same members? And isn't the point of OO that one class should not know (nor care) what members another class has, related or not.

    I know C++ has that "freind" declaration, but I think even Stroustrup admitted afterwards that the whole private/protected/friend/public/static stuff was a mistake.

    "'ll see that if you write accessor methods for the same member with different names in two related classes..."

    How can you have "the same member" in "two ... classes", related or not?

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      Perhaps it helps if i use "attribute" instead of "member", so one can't confuse "member" with "instance"...

      Abigail's proposal is to store attribute values of instances in lexical (or only package-global) hashes to make them safe from subclasses that might clobber attributes that are private to the superclass:

      sub some_method { my $self = shift; $self->{'_very_internal_flag'} = 1; #... }

      A subclass might also need a '_very_internal_flag' (stored as attribute of an instance) and suddenly it all breaks.

      So the idea is to store the attribute data in kind-of "registry": a central repository that keeps thes attributes in it's defining class.

      My first thought on that was to write a module that is a superclass and contains such a registry for all classes. It gives you two methods: get( attribute ) and set(attribute,value) (probably with diofferent names). These methods get/store these values in that central registry and determine the class, in which that actual attribute was defined and thus must be kept apart from other descending classes defining the same attribute. I thought of something like that because of my lazyness: it would save typing for many classes based on that principle.

      Such a module is easy to write:

        Maybe I'm missing something with your implementation but wouldn't something like:
        package Rover; #a class of it's own ;) sub get_set_color { my $self = shift; if( @_ ){ $self->oo_set( 'color' , $_[0] ); } else { $self->oo_get( 'colour' ); } }
        lead to the same problem that Abigail-II was talking about in the first place?