There seem to be main three kinds of modern interfaces for events:
eventname1 => CODEREF,
eventname2 => CODEREF, ... in a call to something, thereby declaring those as the handlers.sub eventname1 { ... } sub eventname2 { ... } ... $object->eventname1(...), $object->eventname2(...), ...
and that object need not be a subclass of anything specific.Now, my question is: does anything but SAX use style 3 there?
POE (Score:2)
I do... (Score:1, Insightful)
Taking this approach helps a great deal if you start looking at techniques like mock objects for testing, or facade type objects that delegate everything.
Don't
Optimize for the common case (Score:2)
SAX uses both 2 and 3. It uses 2 for drivers and filters, and 3 for simple handlers.
The reason this was so is totally deliberate: the most common case in SAX is a handler. With that approach, you only need to provide a (possibly fake) constructor and define those handlers you need. That could have been achieved using approach 2, but that might have gotten in the way of filter chains.
As for other examples of the same pattern, many GUIs and MVC approaches use it. I've seen it on many an occasio
-- Robin Berjon [berjon.com]
Java interfaces (Score:2)
Style 3 is Java interfaces. I loved the concept when I first heard about them, but apparently most older books don't focus on them. (Apparently Java, like Perl, has a huge backlog of obsolete information, advice, and books floating around out there.) I guess interfaces made it into SAX via Java.
Basically, an interface is like a virtual class, except there's no inheritance involved. Interfaces can be used to give you the benefits of multiple inheritance without actually allowing such. For example, to
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re:Java interfaces (Score:2)
Yes, that's very close to the idea. And in fact, there's a growing number of classes that implement the SAX interface while not having SAX as their central focus. They just want to be usable as SAX handlers to build their internal structure.
The way we did it is however slightly different as you are not forced to implement anything of the interface. In fact, you could provide any object as a SAX handler and it will still work, even if it implements none of the interface (quite simply, nothing will
-- Robin Berjon [berjon.com]