Registered Socket Functions
Now that you know how to dynamically cast variables at runtime, we can create a very cool static class I dub RegisteredSocketFunctions. The purpose of this class is to allow any function to be “registered,” which, by doing so, allows global access to the function directly.
The goals of the RegisteredSocketFunctions class:
- Any function in any class can be very easily “registered.”
- Once registered, any function in any class can be executed, maintaining all parameter functionality.
- Any implied or direct usage of the “this” expression in the executed function will be interpreted properly.
Here’s the API:
- * Adds a new function to the list of available socket function calls.
- * @param sFName Socket function call string reference name.
- * @param funcReference A Function reference to the actual function.
- * @param thisObject A reference to the object that should be the "this" object when this function is executed.
- * @usage RegisteredSocketFunctions.registerFunction("myTestFunction", myTestFunction, this);
- public static function registerFunction(sFName:String, funcReference:Function, thisObject:Object):void
- * Removes a function from the list of registered socket function calls.
- * @param sFName The socket function call that should be removed.
- * @usage RegisteredSocketFunctions.unregisterFunction("myTestFunction");
- public static function unregisterFunction(sFName:String):void
- * Executes a Function that was previously registered using <code>registerFunction</code> with the name <code>sFName</code>.
- * @param sFName The string reference name of the function that is to be executed.
- * @param argArray The optional list of parameters that should be executed with the function.
- * @param thisObject The optional object to which the Function referenced by sFName is applied. The "this" operator for the executed function will be <code>thisObject</code> if <code>thisObject</code> is non-null; otherwise, it will be the <code>thisObject</code> specified when registerFunction was called.
- * @return Returns whatever value (if any) is returned by the function being called.
- * @usage RegisteredSocketFunctions.registerFunction("myTestFunction", [var1, var2]);
- public static function executeFunction(sFName:String, argArray:Array = null, thisObject:Object = null):*
Let’s say you have the following class
- import com.natejc.utils.communication.RegisteredSocketFunctions;
- public class RSFTestClass
- private var _sName:String;
- private var _nAge:uint;
- public function TestClass()
- RegisteredSocketFunctions.registerFunction("testFunc", testFunc, this);
- } // END CONSTRUCTOR
- public function testFunc(sName:String, nAge:uint):String
- this._sName = sName;
- this._nAge = nAge;
- trace("Hey " + _sName + ", I can't believe you're " + String(_nAge) + " years old!";
- return sName + String(nAge);
- } // END FUNCTION testFunc
- } // END CLASS TestClass
- } // END PACKAGE
Then any other class in any other function could execute testFunc by doing the following:
- var s:String = RegisteredSocketFunctions.executeFunction("testFunc", ["Joe Mama", 90]);
- // which traces:
- // Hey Joe Mama, I can't believe you're 90 years old!
- // Joe Mama90
This class is intended to allow execution of functions across a network (which we will be discussing further later). Although this may seem like a cool, time saving way to allow classes all over your code interact with each other, doing so is definitely poor coding practice. Just like in real life, a child should never have command over its parent ;). Having said that, if your project is due in 1 hour and you don’t have time to write all of the event handling that you should write, well, congratulations; you just found a class that will encourage your bad habits…
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 3.0 United States License.