Static Classes

Static classes are life savers for those who want a central data hubs where all shared variables are stored and accessed and modified. Some programmers think that this is dangerous since there is no real control over who gets to do what with the variables.. but for a scenario where several objects need to access the same data.. I can’t think of better ways of doing so.

here’s an example of a static class:

package com.packageName{

	public class DataStore{
		static var _DS:DataStore;
		static var dataArray:Array;

		public function DataStore(){
			// write something here
		}

		public static function getInstance(): DataStore	{
			if (_DS == null) _DS = new DataStore();
			return _DS;
		}

		public function doSomething(){
			// write code here
		}
	}
}

and then to call the function doSomething from this class, this is the way it’s called:

com.packageName.DataStore.getInstance().doSomething();
This entry was posted in Actionscript 3.0, Code Snippets, Other. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>