develooper Front page | perl.perl6.language | Postings from July 2001

Good example of object inheritance's usefulness

From:
Michael G Schwern
Date:
July 3, 2001 01:11
Subject:
Good example of object inheritance's usefulness
Message ID:
20010703041157.D24224@blackrider
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 <schwern@pobox.com> -----

From: Michael G Schwern <schwern@pobox.com>
To: Adam Turoff <ziggy@panix.com>
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   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     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   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
This is my sig file.  Is it not nify?  Worship the sig file.
	http://www.sluggy.com



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About