|We don't bite newbies here... much|
Re: Building an anonymous subroutineby stephen (Priest)
|on Aug 13, 2002 at 03:22 UTC||Need Help??|
Sometimes a closure is an object waiting to happen. (And sometimes the reverse.) Consider:
That defines your base class, where all the common code can live.
Then, for each kind of encoder, you can define a subclass. For many subclasses, they'll be doing the same overall thing with variations, and you can just redefine the 'other_stuff' method. Otherwise, you can redefine 'encode' itself... but you'll still have 'common_stuff' to call if you want it.
Then, finally, you only need a factory subroutine to create the right type of encoder for each user preference:
And, finally, to get your new encoder and use it:
There will be a speed hit, but it'll be faster than doing constant per-word checks, and easier to keep organized than closures-upon-closures-upon-closures.
Note: Code is untested and I've been drinking Cabernet.