Refactoring closures to strategies
Sometimes you need to expose a feature of your API as a function reference in order to allow the execution of that feature in a custom way.
This kind of functions are known as closures.
A closure is basically a function reference that is executed within a particular scope, being able to access any scope member through argument passing.
This feature is very powerful as it enables the injection of external logic into an encapsulated scope, but has the drawback that there’s no way of knowing whether a given function will satisfy its target closure’s signature.
Languages like C# have a feature called delegates that enable closures to be type safe. ActionScript does not, so we have to make use of other OOP features in order to overcome the limitations that the use of closures impose us.
Read the rest of this entry »