So we all know that AS3.0 shares the object oriented, class-based love. However, there’s one thing I’m not grasping fully and that is how to get two classes to talk to one another.
Let’s say I have a custom-made Debugger class, and its purpose is to store messages from the code (non-critical errors, such as which data item is being processed at a given point in time), with the potential to display it to a debug “log window”.
Now I have another custom-made class, let’s say Inventory, and in this case we want an instance of the Inventory class to log a message to an instance of the Debugger class.
var myDebugger:Debugger = new Debugger(stage.logWindow);
var myInventory:Inventory = new Inventory('fork', 'spoon', 'knife');
myInventory.retrieveItem([COLOR=#ff0000]'spoon'[/COLOR][COLOR=#000000])[/COLOR];
At that point, in this theoretical example, I would like the myInventory instance to log a debug message eg. “Inventory: Retrieving item ‘spoon’” or similar. However, how do I interact with the myDebugger class?
It is certainly a bad move to hard-code in the expectation that there be an instance of the debugger class called myDebugger.
Should I write a custom event listener? Should I use myInventory inside a try / catch / finally with a throw that is then passed to the myDebugger?
What is the best practice when getting two separate class instances to talk to one another?