AS3 Singleton - FDT Template

Ok, here we go again, AS3 and Singletons have been discussed almost as much as Peak Oil, but never-the-less, allow me to present yet another Singleton implementation, wrapped up in a neat little FDT template.

package ${enclosing_package} {
	
	/**
	 * @author ${user}
	 */
	public class ${enclosing_type} {
		
		private static var _instance : ${enclosing_type};
		private static function hidden() : void {}

		public static function getInstance() : ${enclosing_type} {
			if( _instance == null ) {
				_instance =
					new ${enclosing_type}(hidden);
			}
			return _instance;
		}

		public function ${enclosing_type} (h:Function) {
			if (h !== hidden) {
				throw new Error( "${enclosing_type} and can only be accessed through ${enclosing_type}.getInstance()" );
			}
		}
		
		${cursor}		
	}
}


Download the FDT template XML

Add comment August 12th, 2008

FDT Template for Getter / Setter methods.

If you’re being a good Flex / AS3 dev, you are probably wrapping your class member properties with public getter and setter methods.

If you’re not, you really, really should…

Getters and Setters, otherwise known as accessor methods, or mutator methods (among other things), are very helpful. They allow you to make your public properties have a far greater set of controls and features. I won’t go into all the details here, I’m assuming that you, like me, only fail to set them up because typing…

public var classMember : String;

Is slightly quicker than typing…

private var _classMember : String;

public function set classMember( value : String ) : void {
        _classMember = value;
}

public function get classMember( ) : String {
        return _classMember;
}

Anyway, if you are an FDT user, (or your chosen IDE has a templating feature) you can create a template to do this for you.

Read/Write member … (template name member)

private var _${name} : ${type};

public function set ${name}( value : ${type} ) : void {
        _${name} = value;
}

public function get ${name}( ) : ${type} {
        return _${name};
}

And then it will only take as long as setting up a public var, but with all the goodness that comes with using getter/setters…

You could setup templates for a read only member, and a write only member too…

Read only member … (template name rmember)

private var _${name} : ${type};

public function get ${name}( ) : ${type} {
        return _${name};
}

Write only member … (template name wmember)

private var _${name} : ${type};

public function set ${name}( value : ${type} ) : void {
        _${name} = value;
}
Download the FDT template file.

Add comment July 30th, 2008