AS3 design mode-Singleton mode

Source: Internet
Author: User

Definition: In object-oriented programming, a class is required to have only one instance, provide a global access method, avoid constructors, and provide a mechanism to implement a single instance.

Function: For example, I need to refer to the properties of a class in many classes in order to not instantiate the class to be referenced in each class, then the class to be referenced as a singleton pattern can be avoided when the call is made.

Using the singleton pattern (Singleton pattern) in ActionScript 3.0 can be so colourful! After reading "Advanced ActionScript 3 with Design Patterns", I realized that the singleton pattern I wrote before was not a real singleton mode, or an imperfect singleton (at least not in ActionScript 3.0). The so-called Singleton is an instance of the entire application that has only one class, meaning that a class can only be new once.

To get an instance of a class, you would use the New keyword and then add the class name. This invokes the constructor of the class and returns an instance, just like this:

var myobject:myclass = new MyClass ();

I believe you are already familiar with the way this class is instantiated, but this method does not control the construction of the class, so we are going to use a static method: GetInstance (). Because it is declared static, it is an instance of a class rather than a class, and it is invoked directly using the class name without invoking it through an instance of the class. Just like this: \

public class MyClass {public        function MyClass () {} public        static function getinstance (): MyClass {                return new MyClass ();        }}

To get an instance of the class, we can use this static getinstance () method:

var myobject:myclass = myclass.getinstance ();

Prevent classes from being instantiated
As long as the instance can be obtained through the getinstance () method, all the work has been done as planned. However, we note that this does not always prevent others from creating another instance through the New keyword. In some languages, constructors can be declared as private (only in-package constructs), which solves this problem. In ActionScript 3.0, however, this is not supported. In this case, we can only put a large "public" keyword to declare that the constructor is common, and then write the comment to tell others: "This class can only be instantiated once." Is there no other way to prevent a class from being instantiated? Everything is possible, of course there are. We can use object-oriented rules of the game to achieve the goal, improper grammar is forbidden.
if the parameter of a function does not have a default value, and the function is called without passing arguments, the compiler will error. This is a new feature of ActionScript 3.0, which also applies to constructors. ActionScript 3.0 also has a new feature in which you can write more than one class in a class file. This means that multiple classes can be written in an. as file. The same class as the file name is called the main class, the main class can have only one, the other classes may be any number, and the other classes are only visible within the package and cannot be externally referenced. just like this:

Package {public     class MyClass {public         function MyClass (enforcer:singletonenforcer) {} public         static function getinstance (): MyClass {            return new MyClass (new Singletonenforcer ());}}}     class Singletonenforcer {}

Now you can only create an instance of the class by using the following statement, because you cannot create an instance of the MyClass class that is visible only to the package by using the New keyword externally:

var myinstance:myclass = myclass.getinstance ();

Note: In fact, you can also pass NULL as an argument to the constructor to new out a MyClass class instance. Every language is impossible to be perfect, but the above approach has already been able to solve the problem basically in ActionScript 3.0.

Until now, you have not been able to create a singleton, and every time you call the GetInstance () method, a new instance will be created. Below we will learn how to create a singleton.

implementing Singleton and global variables
         What do we need to do to ensure that the MyClass class is instantiated only once? Now, this getinstance () method can be called indefinitely, and then the new MyClass class is created indefinitely, which is tantamount to using a common public constructor. And we just need an instance of the MyClass type, so we should need a global variable to hold this unique instance, and then return this instance each time we call the getinstance () method instead of re-new. Just like this:

Package {public         class MyClass {                 private static var _instance:myclass;                 Public Function MyClass (enforcer:singletonenforcer) {} public                 static function getinstance (): MyClass {                        // Only the static private variable _instance is null when new out of a MyClass,                        //When this static getinstance () method is called the second time, because _instance is not NULL,                        //So new is no longer a second MyClass, but returns the existing _instance directly.                        //This guarantees that there is only one instance of the MyClass type in the World                        if (myclass._instance = = null) {                                myclass._instance = new MyClass (new Singletonenforcer ());                        }                        return myclass._instance;}}         } Class Singletonenforcer {}

  

AS3 design mode-Singleton mode

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.