Singleton & Simulated Download Problems
I’ve been experience an unusual problem when working with singleton classes in the Flash IDE. When testing a movie and using the simulate download my singletons aren’t initalized corrected. The problem occurs because you can’t immiedetly test using simulate download, you first have to test the movie and then run it again to start the simulated download. It appears that the Flash IDE doesn’t reinstantiate static variables (like the ones used in singletons) and thus getInstance will return the singleton object created on the first test without actually reinstantiating the class. Its as if it returns an empty object. I have yet to find an acceptable solution for this, but have found a workaround.
I usually know which object will create the singleton class first, so by adding a boolean variable to the getInstance function I can tell it to call an init function regardless of whether or not you ran it in the intial test movie or the simulated download afterwords.
class com.dcholth.singleton.MySettings {private static var __uniqueInstance:MySettings; private var __myWebsite:String;private var __myName:String; private var __numberOfMySettings; private function MySettings(){init();} private function init(){trace("MySettings Created");__myWebsite = "http://www.dcholth.com/blog";__myName = "D.C. Holth"; } // Delete the else if when using in your final version in production public static function getInstance(firstInit:Boolean):MySettings{if(__uniqueInstance == undefined){__uniqueInstance = new MySettings;} else if(firstInit){init();}return __uniqueInstance;}}
Now when when the first MySettings object is being created I pass the the true parameter with the getInstance() function it will run the initialization functions. It is important that init is declared as private and that the ‘else if’ is removed from the getInstance when using in a production environment or you may experience problems.
No CommentsSingleton Design Pattern
With over twenty different design patterns available, it was difficult to decide which one to discuss first! After giving it some thought, I came to the conclusion that the simplest and perhaps most useful to a beginner Flash developer is the Singleton design pattern.
The Singleton design pattern is in the creational group of design patterns. If you are ever using _root in your code, odds are you could use a singleton class! The Singleton design pattern makes sure that in an entire application the singleton class is only instantiated one time.
You may be asking, “Can’t I just make a static class and only instantiate it one time?” Yes, you can, however, it is up to you to keep track of that object and make sure a class (yours or otherwise) doesn’t re-instantiate it, reseting all your variables.
Lets take a look at how the basic setup of a singleton class. For this example, I’m creating a class called MySettings, which would reflect the settings I would want each piece of the application to know about and access from a central location.
class com.dcholth.singleton.MySettings { private static var __uniqueInstance:MySettings; private var __myWebsite:String; private var __myName:String; private var __numberOfMySettings; private function MySettings(){ trace("MySettings Created"); __myWebsite = "http://blog.dcholth.com/"; __myName = "D.C. Holth"; } public static function getInstance():MySettings{ if(__uniqueInstance == undefined){ __uniqueInstance = new MySettings; } return __uniqueInstance; } }
Notice anything unusual? The constructor is declared as a private function, which limits the ways one can create a MySettings object. To create a MySettings object one calls the static function getInstance which will check to see if a MySettings object has already been created (__uniqueInstance) and will create one if necessary. This insures that only one unique instance of the MySettings class can be instantiated. If another class wants to use the MySettings object they call this code:
var mySettingsObject:MySettings = MySettings.getInstance();
This is useful in ActionScript because we are often times dealing with multiple nested MovieClips and object that all need to interact with each other. But rather than creating one object on the main timeline and trying to keep track of that nesting through calls to _parent, you can get the Singleton class and know that every object is looking at the same one. A singleton class can easily replace the need to use _root and make your classes more encapsulated.
I’ve been working on a PriorityQueue class, which manages the loading and displaying of SWFs based on priority (or immediately if needed). It wouldn’t make much sense to allow the user to have more than one PriorityQueue, so I made it a singleton class that each class shares. I’ll upload the PriorityQueue class as soon as it is completed.
One Comment


