一个简单的Actionscript的单态模式类

      一个简单的Actionscript的单态模式类

                      --------Actionscript 3.0 CookBook(II)

  刚看Actionscript 3.0 CookBook看到2.4节讲创建静态方法和属性的时候,看到了说private static中static最重要的应用是在单态模式下。就想起我以前看Design Pattern书籍,顺便写了个很简单的单态模式类。

 

 package Src { /** * Written by Leezhm, 10th February, 2009 * Contact : [email protected] * * An example of singleton class **/ public class CSingleton { // variable private static var _instance = new CSingleton(); protected function CSingleton() { } public static function getInstance():CSingleton { if (undefined != CSingleton._instance) { return CSingleton._instance; } else { throw Error("Could not create the instace!"); } } } }

 

   (BTW:CSDN的这种代码显示方式期待了好久啊,刚发现的,兴奋ing)

 

单态模式主要应用在一个类只能创建一个实例的情况的一种类设计模式。

 

 

 

 

 

 

 

 

你可能感兴趣的:(设计模式,function,Class,actionscript)