Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

What kind of OO is this?

by YuckFoo (Abbot)
on Jul 14, 2004 at 21:09 UTC ( [id://374444]=perlquestion: print w/replies, xml ) Need Help??

YuckFoo has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Monks,

A Format object creates and holds an instance of its parent object, Format::Real. AUTOLOAD is then used to propagate method calls up to the parent. More of a wrapper than real inheritance I guess.

Seems clean to me, but if I do this, will I be sorry later? Should I be more conventional and use ISAs and SUPERs?

YuckOo

#!/usr/bin/perl use strict; my $as_float_e = Format->new('%8.4e'); my $as_float = Format->new('%8.4f'); my $as_integer = Format->new('%d'); my $as_string = Format->new('%20.20s'); my $var = 1234.5678; print $as_float_e ->format($var), "\n"; print $as_float ->format($var), "\n"; print $as_integer ->format($var), "\n"; print $as_string ->format($var), "\n"; #----------------------------------------------------------- package Format; #----------------------------------------------------------- sub new { my ($pack, $spec) = @_; my $r = {}; $r->{real} = Format::Real->new(); $r->{spec} = $spec; bless $r, $pack; } #----------------------------------------------------------- sub AUTOLOAD { my $r = shift; my $func = (split('::', $Format::AUTOLOAD))[-1]; if (Format::Real->can($func)) { $r->{real}->$func($r->{spec}, @_); } } #----------------------------------------------------------- package Format::Real; #----------------------------------------------------------- sub new { my ($pack) = @_; bless {}, $pack; } #----------------------------------------------------------- sub format { my $r = shift; my ($spec, $num) = @_; sprintf $spec, $num; }

Replies are listed 'Best First'.
Re: What kind of OO is this?
by fergal (Chaplain) on Jul 14, 2004 at 23:15 UTC
    If you rewrite your AUTOLOAD to use the magic &goto; then the stack frame for the AUTOLOAD routine will be erased. This means that the method in Format::Real will behave exectly as if there had been no AUTOLOAD involved. Something like this would do the trick.
    sub AUTOLOAD { my $r = shift; my $func = (split('::', $Format::AUTOLOAD))[-1]; if (my $sub = Format::Real->can($func)) { unshift(@_, $r->{real}, $r->{spec}); goto &$sub; } }
Re: What kind of OO is this?
by simonm (Vicar) on Jul 14, 2004 at 21:41 UTC
    This is wrapper-style delegation, in some contexts also known as a "decorator" or other jargon depending on what you're doing with it.

    It's a perfectly legitimate design choice, although i don't know enough about your problem space to know whether it would be more or less appropriate than regular inheritance in this case.

    If what you're building really has a traditional inheritance relationship at its core, you might want to use ISA/SUPER for compatibility with standard idiom; if you're dynamically reassigning relationships or stacking multiple behaviors together on the fly, delegation is probably more appropriate. Ask yourself: What's the design challenge driving you to separate this code into two objects/classes? Which parts of it do you think will change most often, and how?

Re: What kind of OO is this?
by pg (Canon) on Jul 15, 2004 at 03:16 UTC

    There is really no problem with what you are doing. Instead of taking it a big question as whether that's OO, or what OO it is, better just take a look at the goals you wanted to succeed.

    You might want it to be OO, or you might just want the problem being resolved in a reasonable way. I am not saying that you cannot kill both birds by one stone, but no one is lucky everyday.

    If you really care to make your program OO, then this is not OO, thus it is not a valid question as what type of OO it is. Using bless does not warrant OO.

    But that does not change the fact that your code is quite elegant, and the trick is nice. You had the thing done, no regret.

Re: What kind of OO is this?
by kscaldef (Pilgrim) on Jul 15, 2004 at 15:42 UTC

    There's more to OO than inheritance relations. In fact, most expert OO designers agree that inheritance is overused and that composition is underused.

    The question you should think about is whether Format is a Format::Real or whether it has a Format::Real. You may also want to read about the Liskov Substitution Principle for more on this topic.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://374444]
Approved by NovMonk
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found