Categories
ActionScript Design Patterns OOP Tips & Tricks

Singleton

Here’s one way to implement a Singleton in ActionScript 2.0:

class Singleton {
    // the only instance of this class
    private static var inst:Singleton;

    // keep count of the number of Singleton objects referenced
    private static var refCount:Number = 0;

    // constructor is private
    private function Singleton() {}

    // get an instance of the class
    public static function get instance():Singleton {
        if (inst == null) inst = new Singleton();
        ++refCount;
        return inst;
    }

    // return the number of instance references
    public static function get referenceCount():Number {
        return refCount;
    }
}


The count property is for keeping count of the number of Singleton objects referenced (obviously it is not aware of destroyed objects), and is not really a necessary member of the Singleton pattern.

To use this class, one would write something like this:

var s1:Singleton = Singleton.instance;
var s2:Singleton = Singleton.instance;

Now s1 and s2 are the same instance of the Singleton class.

Categories
ActionScript OOP

private, protected & public

In ActionScript 2.0, there are “private” and “public” modifiers, but there is no “protected”. However, “private” behaves like “protected” – i.e. subclasses and instances can access private members; so there is no true private scope.

To make sure the compiler catches access to private properties/functions, strict-typing has to be used:

class SomeClass {
     private var:somePrivateProperty:String;
 }

// without specifying the type of the instance, the compiler will not
// prevent statements from accessing private properties; therefore,
// o.somePrivateProperty is accessible

var o = new SomeClass();
trace(o.somePrivateProperty);

// by specifying the type, the compiler will catch acess to
// private members:
var o:SomeClass = new SomeClass();

Because type-checking is only performed at compile time, there is no guarantee that private members cannot be accessed at runtime.

By default, if the “public” modifier is left out, the member is assumed to be public.

Categories
News

The beginning of swfoo.com!

Welcome! This marks the first post for swfoo.com!

What is swfoo?

It’s about ActionScript 2.0 development, object-oriented programming, design patterns, software architecture, Rich Internet Applications, Flash, ColdFusion, Flash Remoting, web services, tips and tricks, life, the universe, and other geeky stuff…

What happens to Quantumwave.com?
[Update 2013, 10 years later: This domain is now qwmobile.com]

It is alive and kicking! Because Quantumwave Interactive Inc. is a company site that offers diverse services, it’d be better if the personal blog and Flash focus are kept separately. I’ll slowly migrate the Flash resources over to this site.

In the mean time, hope you’ll enjoy this new site (which is still under heavy construction)!

Dave Yang