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:
Then, each of these would overload add(), for example. Collection::Unique would do something like:our @ISA = qw( Collection::Bounded Collection::Unique Collection::Generic );
And, likewise for all the other decorators.sub add { my $self = shift; my $thing = shift; return undef if $self->contains($thing); return $self->SUPER::add($thing); }
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:
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 add { my $self = shift; my $thing = shift; my $collection = $self->{collection}; return undef if $collection->contains($thing); return $collection->add($thing); }
Any thoughts?sub AUTOLOAD { my $self = shift; my $func = our $AUTOLOAD; $func =~ s/.*:://; return $self->{collection}->$func; }
------
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |