----- Original Message -----
> From: chromatic <[email protected]>
> To: [email protected]
> Cc: Michael G Schwern <[email protected]>
> Sent: Thursday, 27 October 2011, 9:36
> Subject: Re: Event handling: One method per event or one method for all?
>
> On Wednesday, October 26, 2011 at 09:58 PM, Michael G wrote:
>
>> So now the question is one of good event handler design, which I don't
> have
>> experience with. How does one design a good event handler? Is the pattern
>> one method per type of event? Or one method to do the dispatching? Or
>> something else?
>
> I've done this several times. I prefer one method per type of event (hello,
> cheap and easy polymorphism!).
>
> This is also one case in which I find an abstract base class acceptable; the
> null method pattern means never having to call ->can().
Agreed. That's why I tell people that their Test::Class base class should
always start (sort of) like this:
package My::Test::Class;
use parent 'Test::Class';
sub startup : Tests(startup) {}
sub setup : Tests(setup) {}
sub teardown : Tests(teardown) {}
sub shutdown : Tests(shutdown) {}
In a subclass, you *never* have to worry about the order in which the methods
are called or if you can call them. Need a startup method in a subclass? It's
trivial:
sub startup : Tests(startup) {
my $test = shift;
# no-op? You don't care, nor should you care (with caveats, of course)
$test->SUPER::startup;
...
}
It's much cleaner that way.
Cheers,
Ovid
--
Live and work overseas - http://overseas-exile.blogspot.com/
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://blogs.perl.org/users/ovid/
Twitter - http://twitter.com/OvidPerl/