Design mode-monomer (single case) mode 1, Introduction
Starting with this chapter, we'll step through the various design pattern implementations used in JavaScript, where I don't introduce the theory of the schema itself too much, but focus on implementation. OK, start formally.
In the eyes of traditional development engineers, a single example is to ensure that a class has only one instance, the implementation of the method is generally first to determine whether the existence of the instance, if there is a direct return, if not exist to create a return, this ensures that a class has only one instance object. In JavaScript, a singleton serves as a namespace provider, providing a unique access point from the global namespace to access the object.
2, simple monomer and closure monomer
In JavaScript, there are many ways to implement a singleton, the simplest of which is to use the method of object literals, which can contain a large number of attributes and methods:
<! DOCTYPE html><html><head> <meta name= "name" content= "content" charset=" Utf-8 "> <title>JavaScript monomer mode</title></head><body> <script> //Monomer mode (Sigleton)-not new (); because he's already there. /*1, simple monomer--the mode of the object argument * // * var Sigleton ={attr1:true, Attr2:10, Method1:function () {alert ("I am Method 1");},METHOD2: function () {alert ("I am Method 2");} }; alert (SIGLETON.ATTR1); The monomer mode is mainly divided into namespaces, differentiating between different people code var BHX =bhx | | {}; BHX. Sigleton ={attr1:true,attr2:10,method1:function () {alert ("I am Method 1"),},method2:function () {alert ("I am Method 2");} }; BHX. SIGLETON.METHOD1 (); */ /*2, closure monomer---Borrow the closure of the simple body (closure of the main purpose: to protect data) */ //namespace--implementation of block level scopes /* (function test () {alert (11); })()*/ varBHX = BHX | | {}; BHX. Sigleton2= ( function(){ //The function of closures is to encapsulate their own private properties and not allow access to the outside world. Only return is exposed to access. varA1 =true;varA2 =Ten;varF1 = function(){Alert"function F1"); };varF2 = function(){Alert"function F2"); };//To assign a block-scoped object to Sigleton2, return statement. return{attr1:true, ATTR2:Ten, Method1: function(){ returnF1 (); }, Method2: function(){ returnF2; } }; })(); Alert (BHX. SIGLETON2.ATTR1); BHX. Sigleton2.method1 ();</script></body></html>
The code above is pretty good, but what if we want to initialize it only when it's used? For the purpose of saving resources, we can initialize the code in another constructor, as follows:
3. Inert monomers and branched monomers
<! DOCTYPE html><html><head> <title>JavaScript monomer Mode 2</title></head><body><script> /*3, inert monomer---(there are some similarities with the closure monomers) but you can use variables to control what you want to return, not all of them, so it's good for big projects . //Namespace varBHX = BHX | | {}; BHX. Base = ( function(){ //using private variables to control the returned monomer object varUniqueinstance;//undefined //Need a constructor Init method for initializing an object function init(){ varA1 =true;varA2 =Ten;varF1 = function(){Alert"function F1"); };varF2 = function(){Alert"function F2"); };return{attr1:true, ATTR2:Ten, Method1: function(){ returnF1 (); }, Method2: function(){ returnF2; } }; };return{getinstance: function(){ if(!uniqueinstance) {//If not present, create a singleton instanceUniqueinstance =init (); };returnUniqueinstance; } }; })();//bhx. Base.getinstance (). METHOD1 (); /*4, branching monomers--similar to Swich if else, can be used for the differential detection of the underlying framework browser */ varExt =ext | | {};vardiff =true; Ext.more = ( function(){ varobja={//Firefox browser //attribute 1ATTR1:"Firefox properties 1" //attribute 2 //Method 1 //Method 2};varobjb={//attribute 1ATTR1:"IE attribute 1" //attribute 2 //Method 1 //Method 2};return(diff)? OBJA:OBJB; })(); alert (EXT.MORE.ATTR1);</script></body></html>
Know how to implement a single example, but what kind of scenario is better for a single case? In fact, the single case is generally used in the system between the various modes of communication coordination, the following code is a singleton best practice:
4. Best practices
varSingletontester = ( function () { //Parameters: A collection of parameters passed to the singleton function Singleton(args) { //Set the args variable to be the received parameter or null (if not provided) varargs = args | | {};//Set the name parameter This. Name =' Singletontester ';//Set the value of the Pointx This. Pointx = Args.pointx | |6;//obtained from the received parameters, or set to the default value //Set the value of the pointy This. Pointy = Args.pointy | |Ten; }//Instance container varInstancevar_static = {Name:' Singletontester ',//How to get instances //Returns an instance of singletonGetInstance: function (args) { if(Instance = = =undefined) {instance =NewSingleton (args); }returnInstance } };return_static;}) ();varSingletontest = Singletontester.getinstance ({pointx:5}); Console.log (SINGLETONTEST.POINTX);//Output 5
5. Other ways of realization
Refer to the Visible blog: (http://www.cnblogs.com/TomXu/archive/2012/02/20/2352817.html)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
----monomer (singleton) mode of design pattern