Javascript Singleton)

Source: Internet
Author: User

The Singleton mode has three features:
1. This class only has one instance.
2. This class creates this instance by itself (create its own instance object in this class)
3. Publish the instance interface to the entire system.
This is probably the case in Java.
Copy codeThe Code is as follows:
Class Singleton {
// Private, static class instance
Private static Singleton instance = new Singleton ();
// Private constructor (constructor, constructor, constructor)
Private Singleton (){}
// Public, static factory Method
Public static Singleton getInstance (){
Return instance;
}
}

Use Time
Copy codeThe Code is as follows:
Singleton obj = Singleton. getInstance ();

This Singleton class will be instantiated when it is loaded, even if the loader is static. Therefore, a more reasonable way for a single unit with high configuration overhead for resource-intensive scenarios is to delay instantiation (new) until it is used. Lazy loading is commonly used for the units that must load a large amount of data. Modify
Copy codeThe Code is as follows:
Class LazySingleton {
// The initial value is null and is not instantiated for the moment.
Private static LazySingleton instance = null;
// Private constructor (constructor, constructor, constructor)
Private LazySingleton (){}
// Public, static factory method, which is created only when used
Public static LazySingleton getInstance (){
If (instance = null ){
Instance = new LazySingleton ();
}
Return instance;
}
}

The usage is the same as above.
The Singleton mode is one of the most basic and useful Javascript modes. It provides a means to organize code into a logical unit. The code in this logical unit is accessed through a single variable.
A single entity is useful in Javascipt and can be used to divide namespaces to reduce the proliferation of global variables. It can also be used in Branch Technology to handle the differences between browsers.
There are multiple implementation methods for the single-sample mode in Javascript, each of which has its own advantages or disadvantages.
1. The most basic and simplest monomer for object direct quantity implementation
Copy codeThe Code is as follows:
Var Singleton = {
Attr1: 1,
Attr2: 'hello ',
Method1: function () {alert (this. attr2 );},
Method2: function (arg ){}
}

In this way, all the members of the object are accessed by adding a number in Singleton. All members are public and private. When the variable Singleton is executed, it loads (instantiates) itself, that is, non-inert loading.
In addition, there are some risks when using this to access other single members of method1, because the context of method1 does not always point to the Singleton object.
For example, when method1 is used as the event listener, this may point to the dom element and may prompt undefined.
2. Closure implements the monomer of Private Members
Copy codeThe Code is as follows:
Var Singleton = function (){
Var attr = 1, fn = function (){};
Return {
Method: function () {fn ();},
GetAttr: function () {return attr ;}
};
}();

In this method, var defines the private member attribute attr, the method fn, and then returns a public interface method and getAttr. In the future, the method and getAttr of the interface method will remain unchanged. You only need to modify the specific implementation of private attr and fn. Use the following
Copy codeThe Code is as follows:
Singleton. method ();
Singleton. getAttr ();

3. Closures implement the inert instantiation of Private Members.
Copy codeThe Code is as follows:
Var LazySingleton = function (){
Var attr = 1, fn = function (){};
Var obj = {
Method: function () {fn ();},
GetAttr: function () {return attr ;}
};
Function init (){
Return obj;
}
Return {getInstace: init };
}();

As mentioned above: For the monomer that must load a large amount of data, it will not be instantiated until it is used. The usage is as follows:
Copy codeThe Code is as follows:
LazySingleton. getInstance (). method ();
LazySingleton. getInstance (). getAttr ();

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.