Actionscript 3 singleton pattern template for FDT 3 (Eclipse)

6 Mar 09 by Jos

“What? The singleton pattern is gone in FDT3 actionscript 3?” No it’s not! You just have to alter a bit of code to get it to work. FDT didn’t include this template in their version 3. I got the pattern from “Actionscript 3 with Design Patterns“. Here’s the template code to have the CTRL-space-singleton action back again :)

Open FDT preferences, goto templates, then add a new one for AS3 (see pic below)

package ${enclosing_package}
{
	/**
	 * @author ${user}
	 */
	public class ${enclosing_type}
	{
		private static var __instance:${enclosing_type};

		public function ${enclosing_type}(enforcer:SingletonEnforcer) {}

		public static function getInstance():${enclosing_type}
		{
			if (${enclosing_type}.__instance == null)
				__instance	= new ${enclosing_type}( new SingletonEnforcer() );

			return __instance;
		}
	}
}
class SingletonEnforcer {}

6-3-2009-15-27-20

One comment

Leave a comment

Gers? Stuur 'm door!
E-mail vriend(in)
Bericht
Veiligheidscode