Welcome!

Adobe Flex Authors: Liz McMillan, RealWire News Distribution, Maureen O'Gara, Yakov Fain, Keith Swenson

Related Topics: Adobe Flex

Adobe Flex: Article

Macromedia Flash - How To Use the Singleton Design Pattern

A standardized solution to a common programming problem

The implementation of this history class is also simple. The example above uses two navigation components. The following is the class code for those two components:

// SiteNavigation.as
class SiteNavigation extends MovieClip {
private var buttonOne:MovieClip;
private var buttonTwo:MovieClip;
private var buttonThree:MovieClip;
private var buttonFour:MovieClip;
private var history:History;
function SiteNavigation() {
history = History.getInstance();
buttonOne.onRelease = function() {
if(this._parent.history.addItem('One')) {
this._parent._parent.gotoAndPlay('One');
}
buttonTwo.onRelease = function() {
if(this._parent.history.addItem('Two')) {
this._parent._parent.gotoAndPlay('Two');
}
buttonThree.onRelease = function() {
if(this._parent.history.addItem('Three')) {
this._parent._parent.gotoAndPlay('Three');
}
buttonFour.onRelease = function() {
if(this._parent.history.addItem('Four')) {
this._parent._parent.gotoAndPlay('Four');
}
// HistoryNavigation.as
class HistoryNavigation extends MovieClip {
private var backButton:MovieClip;
private var forwardButton:MovieClip;
private var history:History;
function HistoryNavigation() {
history = History.getInstance();
backButton.onRelease = function() {
this._parent._parent.gotoAndPlay
(this._parent.history.previousItem);
}
forwardButton.onRelease = function() {
this._parent._parent.gotoAndPlay
(this._parent.history.nextItem);
}

Conclusion
The Singleton pattern is both a simple and proven design pattern for restricting instantiation and for providing a global access point. A variation of this class is the Multiton pattern, which also restricts instantiation, but allows for a fixed number of instances.

This article was originally published on Community MX (www.communitymx.com).

More Stories By Danny Patterson

Danny Patterson is a Consultant
specializing in Flash and Web technologies. Danny is a Partner and
Author at Community MX (communitymx.com) and a member of Team Macromedia
Flash. He is a Certified Advanced ColdFusion MX, Flash MX and Flash MX
2004 Developer. You can check out his weblog at DannyPatterson.com.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
SYS-CON Belgium News Desk 08/03/05 01:41:16 PM EDT

Macromedia Flash - How To Use the Singleton Design Pattern. Design patterns are standardized solutions to common programming problems. It's good to use design patterns for two simple reasons. First, it's faster (and many times better) to implement a time-tested design pattern as opposed to developing a custom solution. Second, collaboration with other developers increases when common practices are used.