I've decided, because I feel like it, to roll my own Collection:: hierarchy. I'm know there's (at least) one on CPAN, but I want to roll my own, then compare to what "The Experts"(tm) have done before me. Call it a masochistically-inspired learning experience.

I'm also nearly done with reading Design Patterns. So, I'm chock-full of vim'n'vinegar and want to use as many patterns as I can as soon as I can.

So, given that a collection might want to utilize different characteristics (unique, bounded, N-dim, sorted, etc) ... I was trying to figure out the best way to structure the hierarchy.

My first idea was to create some sort of Composite, but I rationalized that away as being too complex for my baby steps.

So, I came with the idea of using Decorators. Sorta like the border on a widget. Widget::Border inherits from Widget::Generic, but isn't a standalone widget. It merely adds behavior (or decorates) the widget.

Now, I'm trying to figure out if this behavior should be class- or object-based.

Class-based would have it be done through @ISA. You, the user, would create a Collection::BoundedUniqueColl and have something like:

our @ISA = qw( Collection::Bounded Collection::Unique Collection::Generic );
Then, each of these would overload add(), for example. Collection::Unique would do something like:
sub add { my $self = shift; my $thing = shift; return undef if $self->contains($thing); return $self->SUPER::add($thing); }
And, likewise for all the other decorators.

To handle it at runtime (which is what Design Patterns suggests), each decorator would actually have an attribute being the object below (which could be the thing itself or another decorator). So, the add would look something like:

sub add { my $self = shift; my $thing = shift; my $collection = $self->{collection}; return undef if $collection->contains($thing); return $collection->add($thing); }
Within Collection::Decorator (which is the base for all Decorators), you'd have to have some sort of AUTOLOAD to handle every other function, sorta like:
sub AUTOLOAD { my $self = shift; my $func = our $AUTOLOAD; $func =~ s/.*:://; return $self->{collection}->$func; }
Any thoughts?

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Implementing Decorators in Perl by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.