Eclipse Template Joy - Sorry, I’m Single

I’ve recently become increasingly enamoured with the joyful wonder that is Eclipse’s code templates - be they in FDT or ASDT, they fill my heart with a warm fuzzy sense of joy and make me tipsy with the raw coding power available to me. So, here’s a code template for my particular flavour of the ubiquitous Singleton Pattern:

/**
 * @author ${user}
 */
class ${enclosing_package_and_type} {

	private static var _instance : ${enclosing_type};

	private function ${enclosing_type}() {
		${cursor}
	}

	/**
	 * @return singleton instance of ${enclosing_type}
	 */
	public static function get instance() : ${enclosing_type} {
		if (_instance == null)
			_instance = new ${enclosing_type}();
		return _instance;
	}
}

This is slightly cleaner than the more traditional version (which ships with FDT) in that it uses an implicit getter function for the instance - this just means that you can write slightly cleaner looking code - for example:

MyManagerClass.instance.myFunkyMethod(myParams)

vs.

MyManagerClass.getInstance().myFunkyMethod(myParams)

Works for me, anyway.

Leave a Reply

You must be logged in to post a comment.