Ziggy came up with a good example of when object inheritance makes
life easier.  Basically, there are times when you'd want to override
individual methods of individual objects.  Example below.


----- Forwarded message from Michael G Schwern <[EMAIL PROTECTED]> -----

From: Michael G Schwern <[EMAIL PROTECTED]>
To: Adam Turoff <[EMAIL PROTECTED]>
Subject: Re: Anyone actually experienced with object inheritance?

On Mon, Jul 02, 2001 at 07:56:27PM -0400, Adam Turoff wrote:
> What I want is a lazy object interface.  I want a mailbox class
> that has a factory method like next() that returns mail message
> objects.  When instantiated, each message has a few members and
> object data containing the unparsed text of the message.  When
> header() or body() is called, then and only then do I want the
> message split, with the header and body going into object members,
> but I also want header() and body() replaced with simple accessors
> *only for this object*.  And I want to do it easily.

Okay, I see where this is going.  You want something like...

        package Mail;
        use base qw(Class::Object);

        sub header {
            my($self) = shift;

            $self->{header} = $self->_parse_header;

            $self->sub 'header', sub {
                my $self = shift;
                if(@_) {
                    $self->{header} = shift;
                }
                return $self->{header};
            };

            return $self->{header};
        }

        sub body {
            ...similar to header...
        }

        my $mail = Mail->new;
        print $mail->header;    # calls the full Mail::header
        print $mail->header;    # calls the simple accessor

Right, I can see how that would be hard to pull off using normal
inheritance.  Object inheritance gives you fine grained control over
overriding individual methods for individual objects.  Clever!


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <[EMAIL PROTECTED]>       Kwalitee Is Job One
<mendel>         ScHWeRnsChweRN    sChWErN   SchweRN  SCHWErNSChwERnsCHwERN    
  sChWErn  ScHWeRn      schweRn           sCHWErN           schWeRn    scHWeRN 
   SchWeRN      scHWErn SchwErn       scHWErn       ScHweRN       sChwern      
scHWerN        scHWeRn           scHWerN        ScHwerN       SChWeRN scHWeRn  
        SchwERNschwERn        SCHwern  sCHWErN   SCHWErN           sChWeRn 

----- End forwarded message -----

-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <[EMAIL PROTECTED]>       Kwalitee Is Job One
This is my sig file.  Is it not nify?  Worship the sig file.
        http://www.sluggy.com

Reply via email to