ActionScript 3 AS3:单例解释

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3:单例解释相关的知识,希望对你有一定的参考价值。

/* Note:
 * Notice that both _instance & getInstance() are static.  Thus, _instance exists before
 *   any objects of the class have been created, and getInstance() can be called for the 
 *   class and so it can be called before there are any objects.
*/
package mvc
{
	import mx.collections.ArrayCollection;

	/* Because ActionScript requires that all constructors are public, you can't prevent
	 *   anyone from making more Model Objects.  Even without an explicit constructor, it
	 *   would appear as if it can't happen but unfortunately, but the compiler will still 
	 *   synthesisize a default public constructor.  Thus singleton behavior can only happen
	 *   by convention and the best you can do is communicate the intent of the pattern
	*/
	public class Model
	{
		private static var _instance:Model;
		
		[Bindable]
		public var buddies:ArrayCollection = new ArrayCollection( [ "ChrisAIV", "Jimmy", "Sandy", "Frosie" ] );
		
		public static function getInstance():Model
		{
			if( !_instance ) _instance = new Model();
			return _instance;
		}
	}
}

以上是关于ActionScript 3 AS3:单例解释的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3 SWFtrospection(as3swf)

ActionScript 3 AS3 TextField和StyleSheet示例(在ActionScript中创建)

[ActionScript 3.0] AS3.0 水面波纹效果

ActionScript 3 AS3:使用SWFObject2和AS3传递变量

ActionScript 3 克隆数组(AS3)

ActionScript 3 AS3序列化