ActionScript Design Pattern Samples: The Singleton
I’ve decided to write a short blog series on ActionScript design patterns. This series will NOT contain in-depth discussions about the patterns, when to use them, why to use them, or any of that. Rather, each post will have a working example that demonstrates a pattern in use, and will be accompanied by a brief description of key points. These working samples may serve as a functional reference for myself or anyone else who may find them useful. Enjoy.
The Singleton Pattern
The Singleton Pattern is a very commonly used pattern in ActionScript; Cairngorm’s ModelLocator is one of the most popular examples that people may be familiar with. In short, a Singleton is used when you want to guarantee that only one instance of a class can exist.
Why would you want this? Well, to avoid conflicts for one. Imagine you have an application with a shopping cart, and various items can be added to the cart from different sections of your application. You’d typically want all a user’s products in a single shopping cart. If multiple carts existed each would have their its products, its own total, and it would be difficult (and problematic) to manage them all at checkout. So how could you ensure that only one shopping cart exists, especially if you have multiple developers working on the different sections of the application? The answer is to make the shopping cart class a Singleton, which ensures that only a single instance of the Singleton class can exist.
The Code
So, how can the Singleton pattern ensure that only one instance of a class exists? Simple, limit access to the class’ constructor and require the class to instantiate itself. This is typically done with a private constructor in other languages, but ECMAScript, the current standard that ActionScript 3.0 is based on, does not support private constructors. So instead, we create a class like the following:
-
Prohibit constructor access: The easiest way to prevent access to a class’ constructor is to require a parameter that only the class itself has access to. In the example, the SingletonEnforcer class is declared in such a way that it cannot be accessed by any class other than
ShoppingCart. Then, by requiring the SingletonEnforcer class to be passed into its constructor, theShoppingCartclass ensures that it cannot be instantiated by anything else; it’s responsible for it’s own creation. -
Provide a single entry point: Since Singleton classes cannot be instantiated directly, there needs to be an entry point for external classes to request an instance. The
getInstance()method serves this purpose. It must be a static method for it to be exposed without requiring an existing instance of the class. This method simply checks to see if an internal instance exists (stored as theinstancevariable), creates an instance if not, and then returns that single instance.
The Sample
And here’s a working example, you can view the source code here.
And that’s it. Please let me know if you have any questions, comments, or suggestions.
Nice example Kalen. Should also point out to those who don’t know, because AS3 is single-threaded you don’t have to worry about threading issues as you do with Java and other languages. In a threading language, you would have to worry about two threads calling getInstance() at the same time and creating two instances of ShoppingCart.
Hey Kalen,
Sorry to reach out through blog comments
But wanted to see if you’d be interested in doing a Flex Show interview on printing in Flex.
john AT theflexshow DOT com
Thanks man!