in reply to Decorator(?) classes and inheritance

I don't have the gang of four here at my cubicle, but I do develop code which uses decorators.

Decorator B should not "inherit" anything from A; they are more of a 'friend' relationship. In Perl, you would probably end up munging the symbol table to accomplish it. Decorators are added after-the-fact, per-instance, upon demand. So SOME instances of A also have B-like qualities, and SOME instances of A do not.

Another way of saying it: You don't really say "make a new instance of a B," but instead, "make a new instance of an A, and then attach some B-ness to it."

my $a = new A(); $a->bee(); # A::bee() does not exist or has no effect $a->attach('B'); # make A load B and attach B methods $a->bee(); # A::bee() now exists thanks to B::bee

This approach would still hold true, even if you make another Decorator like C. Some A instances may have B, some have C, some have both B and C, and some have neither.

Update: You can then make a D class which inherits from A, but always attaches B-ness and C-ness.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re^2: Decorator(?) classes and inheritance
by adrianh (Chancellor) on Jan 30, 2004 at 16:42 UTC
    Decorator B should not "inherit" anything from A; they are more of a 'friend' relationship.

    Actually, it's common for decorator classes to inherit from the base class that they decorate. That way they get the same interface, isa works, etc. The examples in the GOF book do exactly this.