in reply to Class or Object composition ??

The example you give is not appropriate for inheritence, since a Server is not a SendRecv, and instead it is more like a Server does SendRecv. This is the role model of class composition, sometimes called allomorphism (at least thats what chromatic always calls it).

You might want to take a look at Moose, and in particular the (Perl 6 style) role features. Alternately there is Class::Trait or Perl6::Roles as well (there are some other "Role" modules on CPAN, but they are either incomplete or incorrect).

As for composing the class vs. composing the object, I think this is probably something which should be dictated by the requirements. If you really need to have multiple instances all of which can do different types of servering, then use object composition. But the minute you might have several instances of one type of server, you might want to consider going with class compostion, and making custom subclasses for your needs.

As for how you can do the object composition (if you choose that route), the Class::Trait module has a stable "runtime trait composition" feature which Ovid added several months ago. Perl6::Roles has that feature as well (and in fact was the primary reason dragonchild wrote that module). Moose only recently added this feature, and it is completely undocumented (aside from a test script in the t/ directory), so unless you really want Moose's other features, I would not recommend it yet.

-stvn

Replies are listed 'Best First'.
Re^2: Class or Object composition ??
by aufflick (Deacon) on Oct 04, 2006 at 08:12 UTC
    ++ good answer to a good question
Re^2: Class or Object composition ??
by rootcho (Pilgrim) on Oct 04, 2006 at 22:49 UTC
    thanx,
    After I read some of the documetns i went to Class::Delegation, which seems to be what I named "runtime overhead" in my post. ;)
    So it seems that combination of Perl6::Role, Class::Delegation and inheritance can solve most of the problems and allow the design to be easily changed if a need arises on the fly.
    As a side question - Does the new Roles in perl6 cover the delegation-pattern ?
      As a side question - Does the new Roles in perl6 cover the delegation-pattern ?

      Yes, although not as extensively as Class::Delegation. You can see examples of it here. In addition Moose also supports Perl 6-ish style delgation like so:

      package Tail; use Moose; sub wag { ... } package Dog; use Moose; has tail => ( is => 'ro', isa => 'Tail', default => sub { Tail->new }, handles => { 'wag_tail' => 'wag' } ); ## later in your code ... my $fido = Dog->new; $fido->wag_tail; # is equivalent too $fido->tail->wag;
      Its seems to me that the main difference between Perl 6/Moose style delegation and Class::Delegation is the options for combining methods that Class::Delegation offers.

      -stvn