design patterns and hooks

So I found a really good article on design patterns with hooks - a quick excerpt

The methods invoked by template methods are called hooks. Hook methods are places where adaptation can occur by subclassing. It's the job of hooks (and especially hook subclasses) to define, augment, or override each step in the algorithm. Hook methods are not to be confused with hook operations, which are defined in the next pattern. Hook operations are places, back-doors if you will, where external programs may be explicitly called to augment or override an existing method without subclassing.

The Template Method pattern, which is the interaction between template methods and hooks, is found in, among other places, all patterns that deal with abstract classes and abstract methods.

I'm currently working on adding in hooks for phpfanfiction - the biggest problem is how to allow hooks to be registered without a global variable from hell, which is just annoying. Currently I'm working on a solution that would allow several kinds of hooks - sitewide pre-hooks, sitewide post-hooks, modulewide pre-hooks, modulewide post-hooks - those aren't too hard to figure out because I can hook them into the framework class and the abstract class the modules have to extend, so far so good. But I'd like to allow finer grained hooking as well, at the action(class method) level - override hooking, where you replace one of the actions with your own method, and pre and post hooks for a specific action - YUCK. So I'm fighting with the code to get it to work properly and still ending up with registering code.

I've also set up a "data-cache" area that so far caches retrieved registry values, and will later cache registered hooks for speed help. But this will make the thing pretty slow if a stupid safe-mode provider won't allow writing files, oh well.

Comments

Be the first to write a comment!

Post a Reply