I have a class which at the moment is quite messy/repetitive:
public class AvFramework extends Object
{
// vars
private var _handler:AvHandler;
private var _keyboard:AvKeyboard;
private var _manager:AvManager;
/**
* Constructor
*/
public function AvFramework()
{
_handler = new AvHandler();
_keyboard = new AvKeyboard();
_manager = new AvManager();
// attach
_handler.framework = this;
_keyboard.framework = this;
_manager.framework = this;
}
/**
* Getters
*/
public function get keyboard():AvKeyboard{ return _keyboard; }
public function get manager():AvManager{ return _manager; }
}
This class is only going to need to make use of more and more classes, and I don’t really want to have 3 huge lists for this like above.
Is there a way to do the above dynamically – maybe using getDefinitonByName() in a loop of strings to represent the classes I want to create.
I also want the properties to be read-only and to be accessed via framework.myDynamicVarHere.
I’m thinking something along these lines:
- I create a list of the classes I want to create instances of, paired with the variable name they should be accessed by.
- I will need to make the class
dynamicso that I can set the vars viathis["var"] = new Blah();
Quick snippet of where my thoughts are going:
var required:Object =
{
keyboard: "avian.framework.background.AvKeyboard",
manager: "avian.framework.background.AvManager",
handler: "avian.framework.background.AvHandler"
};
var i:String;
for(i in required)
{
var Req:Class = Class(getDefinitionByName(required[i]));
this[i] = new Req();
AvFrameworkObject(this[i]).framework = this;
}
Just not sure how I would be able to make these read-only.
You can use Proxy class to control get/set call and complete Eugeny89 answer.
Look at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html or http://blog.joa-ebert.com/2007/05/25/as3-proxy-example/
With this code, when you do myAvFrameworkObj.something, getProperty() is automaticaly call and you get property name from “name” variable.