Let's say you have an object oriented html form validation library and, beside all the default checks (like non-empty or is_number etc.), you want the users of your library to add their own checks coded in the native programming language of your library. You have two simple ways to do that - you can let the form object contain a callback to be used for the check or you can use the Template Method pattern and let programmers subclass your form (or maybe individual field) class and implement their own 'check' method. Both solutions have their own pros and cons but I've never seen a comprehensive comparison of them.
What do you think about them? It seems that some programmers groups prefer one solution over the other (it's hard not to notice how callbacks are popular in the Plack related stuff). Callbacks are simpler, you don't need additional classes there, they don't complicate the program structure, but they are also more constrained. Template Methods are more structural. You can make them call other template methods and make that whole thing fine grained - that is harder with callbacks. In FormHandler I wanted to be able to stack the many generic checks together into lists - but such lists are not very reusable, you cannot override parts of it like you could if they were named methods. The point is that this additional class is another indirection layer and as we know that all programming problems are solvable with in this way :)
Monday, June 27, 2011
Subscribe to:
Post Comments (Atom)
1 comment:
Generally I use a callback for the first few iterations, since it is not always so clear what the object might really be and I would prefer to not prematurely architect a system.
However, strictly speaking, a well designed system, using callbacks designed with good functional programming standards, should be as extensible and reusable as an )) styled system. Just that in Perl we tend to associate namespaces as an organization of objects, so we tend to reuse OO code more than functional code.
Post a Comment