First, the singleton mode is one of the object's creation modes, and it also includes the Factory mode. The three characteristics of the singleton mode:
1, there is only one instance of the class
2, the class creates the instance itself (creates its own instance object within the class)
3, exposing this instance interface to the entire system
This is probably what it looks like in Java:
Class Singleton { //private, static class self instance private static Singleton instance = new Singleton (); Private constructor (constructor, constructor, constructor method) private Singleton () {} //Public, static factory method publicly static Singleton getinstance () { return instance;} }
When used:
Singleton obj = singleton.getinstance ();
This singleton class is instance instantiated when it is loaded, even if the loader is static. Therefore, it is more reasonable to defer instantiation (new) until it is used, for resource-intensive, more expensive-to-configure monomers. Lazy Loading (lazy loading), which is commonly used for monomers that have to load large amounts of data. Modified under:
Class Lazysingleton { //is initially null, the private static Lazysingleton instance = NULL is not instantiated temporarily; Private constructor (constructor, constructor, constructor method) private Lazysingleton () {} //Public, static factory method, need to be used to create the single Lazysingleton getinstance () { if (instance = = null) { instance = new Lazysingleton (); } return instance;} }
Use the same way as above.
Singleton mode is one of the most basic and useful patterns of JavaScript. It provides a means of organizing the code into a logical unit that is accessed through a single variable.
Monomers have many uses in Javascipt, which can be used to partition namespaces to reduce the flooding of global variables. You can also use the branch technology to handle the differences between browsers.
There are many ways to implement the singleton pattern in JavaScript, each of which has its own advantages or disadvantages.
1, the object face to achieve the most basic, the simplest monomer
var Singleton = { attr1:1, attr2: ' Hello ', method1:function () {alert (THIS.ATTR2);}, Method2:fun Ction (ARG) {}}
In this way, all members of the object are accessed through the singleton dot number. All members are public, without private. When executed to the variable singleton, it loads (instantiates) itself, that is, non-lazy loading.
Also method1 there are some risks in accessing the other members of the monomer with this because the METHOD1 context is not always pointing to the singleton object.
For example, when METHOD1 is used as an event listener, this may point to a DOM element, which may prompt undefined.
2, closures implement private members of the monomer
var Singleton = function () { var attr = 1, fn = function () {}; return { method:function () {fn ();}, getattr:function () {return attr;}} ;} ();
In this way Var defines the private member property attr, method FN, and then returns a public interface method and GetAttr. In the future, when the implementation is modified, the interface method and GetAttr are unchanged, just modify the specific implementation of the private attr and FN. Use the following:
Singleton.method (); Singleton.getattr ();
3, closures implement lazy instantiation of private members of monomers
var Lazysingleton = function () { var attr = 1, fn = function () {}; var obj = { method:function () {fn ();}, getattr:function () {return attr;} }; function init () { return obj; } return {getinstace:init};} ();
The application is mentioned above: For those monomers that must load large amounts of data, they are instantiated only when they need to be used. This is how it works:
JavaScript Base Object creation mode monomer/Singleton mode (Singleton)