Subscribe to
Posts
Comments

Singleton 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 Response to “Singleton Design Pattern”

  1. on 15 Dec 2007 at 5:07 amIdetrorce

    very interesting, but I don’t agree with you
    Idetrorce

Leave a Reply