Singleton mode in as3

Source: Internet
Author: User

Singleton mode is the easiest mode to implement in C #. Its main purpose isRestrictionsUserNewTo createMultiple instances.However, in as3, the constructor must be public (required by the syntax itself) and cannot throw an exception in the constructor (compilation is successful, but the logic does not work ), this is equivalent to completely cutting off the path for creating an instance, and one instance is not available!

ErrorCode:

 
Package {public class Singleton {static private VaR _ instance: Singleton; Public Function Singleton (): void {Throw error ("single-piece mode cannot use new to create an instance! ");} Public static function getinstance (): Singleton {If (_ instance = NULL) {_ instance = new Singleton (); // because the above constructor throws an exception, an error will be reported during running here} return _ instance ;}}}

How can I create instances and Prevent Users From calling constructor? One feature of as3 is used here: by default, only one class can be put in one as file and must be declared using package, but there is a special situation: two classes are defined in an as file. One is declared as a package, and the other is not required!Class without package. The default access range is "only accessible to classes in the same file"

Package {public class singletonfactory {Private Static Var _ instance: singleton2 = NULL; Public Function singletonfactory (): void {trace error ("error! ");} Public static function getinstance (): singleton2 {If (_ instance = NULL) {_ instance = new singleton2 () ;}return _ instance ;}}} class singleton2 {import flash. utils. gettimer; private VaR _ createtime: uint; Public Function singleton2 () {_ createtime = gettimer ();} public function tostring (): String {return "instance Creation Time: "+ _ createtime. tostring ();} public function helloworld (Name: string): String {return "hello" + name + "! ";}}

Test:

Package {import flash. display. sprite; import flash. utils. gettimer; public class main extends sprite {public function main () {var A: * = singletonfactory. getinstance (); trace (gettimer (); var S1: * = singletonfactory. getinstance (); trace (s1.tostring (); // empty loop, deliberately occupying CPU, consumption only for (var I: uint = 0; I <999999; I ++) {// trace ();} trace (gettimer (); var S2: * = singletonfactory. getinstance (); trace (s2.tostring (); trace (S1 = S2); trace (s1.helloworld ("Jimmy "));}}}

But here is a bad thing. The singleton2 class cannot be accessed after it leaves the file singletonfactory. As, so we can only use VaR S1:*Although it can be used, it cannot be automatically perceived by code in FD, FB, and other programming environments!

You can use the interface to improve:

 
Package {public interface isingleton {function tostring (): string; function helloworld (Name: string): String ;}}

Then let singleton2 implement this interface

Package {public class singletonfactory {Private Static Var _ instance: singleton2 = NULL; Public Function singletonfactory (): void {trace error ("error! ");} Public static function getinstance (): singleton2 {If (_ instance = NULL) {_ instance = new singleton2 () ;}return _ instance ;}}} class singleton2 implements isingleton // here it is changed to the implementation interface {import flash. utils. gettimer; private VaR _ createtime: uint; Public Function singleton2 () {_ createtime = gettimer ();} public function tostring (): String {return "instance Creation Time: "+ _ createtime. tostring ();} public function helloworld (Name: string) : String {return "hello" + name + "! ";}}

Retest:

 
Package {import flash. display. sprite; public class main extends sprite {public function main () {var S: isingleton = singletonfactory. getinstance (); trace (S. helloworld ("jimmy. yang "));}}}

Of course, after understanding the above principles, it can be further simplified. Since the package class is not declared, only other classes in the same file can be accessed, why not use it as a constructor parameter? (Does this limit the external call of Constructors)

Package {public class singleton2 {Private Static Var _ instance: singleton2; Public Function singleton2 (N: _ Nothing) {} public static function getinstance (): singleton2 {If (_ instance = NULL) {_ instance = new singleton2 (New _ Nothing ();} return _ instance ;}} class _ Nothing {}

In this way, it is much more refreshing. Of course, there are more than one implementation method of the singleton mode in as3. the following may be easier to understand:

 
Package {public class Singleton {Private Static Var _ instance: Singleton = NULL; Public Function Singleton () {If (_ instance = NULL) {_ instance = This ;} else {Throw error ("an instance of this class already exists! ") ;}} Public static function getinstance (): Singleton {If (_ instance! = NULL) {return _ instance;} return New Singleton ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.