Hello,
I’m a bit new at OOP and am trying to implement a Singleton that parses some XML into an object to use throughout my application. It is my understanding that getInstance() returns a reference to the object returned by the constructor. How then can I access the public methods of the returned object once its been instantiated? Calling the Singleton’s public methods returns a type error saying they are possibly undefined. Furthermore I am trying to an register an eventListener to notify me when the Singleton is done parsing the XML so I can continue building my application when that data is ready.
Any help would be greatly appreciated.
Thanks!
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class CustomXmlParser extends EventDispatcher{
private static var ldr:URLLoader = new URLLoader();
private static var instance:CustomXmlParser;
private static var allowInstantiation:Boolean;
private static const myString:String = 'hello';
public function CustomXmlParser():void
{
if (!allowInstantiation){
trace('Error: Instantiation failed: Use CustomXmlParser.getInstance() instead of new');
} else{
dispatchEvent(new Event("XML_COMPLETE"));
}
}
public static function getInstance():CustomXmlParser
{
if (instance==null){
allowInstantiation=true;
instance = new CustomXmlParser();
allowInstantiation=false;
}
return instance;
}
public static function getString():String
{
return myString;
}
}
}
// ApplicationMain.as
import flash.events.Event;
import flash.display.MovieClip;
public class ApplicationMain extends MovieClip{
private static var temp:CustomXmlParser = CustomXmlParser.getInstance();
public function ApplicationMain()
{
trace(temp) // traces([object CustomXmlParser])
trace(CustomXmlParser.getString()); // traces 'hello'
temp.getString(); // error, call to possibly undefined method
temp.addEventListener('XML_COMPLETE', onXmlComplete);
// says addEventListener method is unknown
}
private function onXmlComplete(evt:Event)
{
trace('ready to move on');
}
}