JS and design patterns ------ Singleton

Source: Internet
Author: User

JS and design patterns ------ Singleton
I. general summary 1. It is not difficult to understand the singleton mode as the name suggests. It is the only instance that generates a class and will be used in our actual development, it is a creation mode. Based on the syntax features of the JS language itself, the object quantity is "{}", which can also be used as a form of Singleton mode, for the following code, refer to 1 function Foo () {2 this. bar = "Hello Singleton! "; 3}; 4 var Singleton = {5 instance: null, 6 getInstance: function () {7 if (! This. instance) {8 this. instance = new Foo (); 9} 10 return this. instance; 11} 12}; like this when we call Singleton each time. getInstance () is used to obtain a unique instance. 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. Then let's look at several examples (* ^__ ^ *) 1 var Singleton = (function () {var instance = null; 2 function Foo () {3 this. bar = "Hello Singleton! "; 4}; 5 return {6 getInstance: function () {7 if (! Instance) {8 instance = new Foo (); 9} 10 return instance; 11} 12}; 13}) (); this is achieved through the module mode. 1 var Singleton = (function () {var instance = null; 2 function Foo () {3 this. bar = "Hello Singleton! "; 4}; 5 function createInstance () {6 return new Foo (); 7}; 8 return {9 getInstance: function () {10 if (! Instance) {11 instance = createInstance (); 12} 13 return instance; 14} 15}; 16. Singleton mode is one of the simplest forms of design patterns. This mode aims to make an object of A Class A unique instance in the system. To achieve this, you can start by instantiating the client. Therefore, we need to use a mechanism that only allows the generation of unique instances of the object class to "Block" access to all objects to be generated. Use the factory method to restrict the instantiation process. This method should be a static method (class method), because it is meaningless to let the class instance generate another unique instance. 2. For the source code case, refer to the Bootstrap front-end framework as an example. The source code in "alert. js v3.3.1" is as follows (! Data) $ this. data ('bs. alert ', (data = new Alert (this) ". this is the Instance Object for creating a component in a singleton mode. You can find the object cached in the DOM node to determine whether to create an instance. Here are a few examples to illustrate how the singleton mode is organized and how to use it again. This is a kind of inert loading. Iii. Introduction of cases today we will make a small example based on the singleton mode and the factory mode, focusing on understanding. (1) create a Singleton Class 1 var Singleton = (function () {2 var instance = null; 3 function createInstance (type) {4 return factory. create (type); 5}; 6 return {7 getInstance: function (type) {8 if (! Instance) {9 instance = createInstance (type); 10} 11 return instance; 12} 13}; 14}) (); (2 ), create a factory class 1 var factory = (function () {2 var instanceVendor = {3 "foo": function () {4 return new Foo (); 5 }, 6 "zoo": function () {7 return new Zoo (); 8} 9}; 10 return {11 create: function (type) {12 return instanceVendor [type] (); 13} 14}; 15}) (); (3), create Object Class 1 function Foo () {2 this. bar = "Hello Singl Eton! "; 3 this. getBar = function () {4 return this. bar; 5}; 6}; (4), create the client test Class 1 function SingleClient () {2 this. run = function () {3 Singleton. getInstance ("foo "). getBar (); // Hello Singleton! 4}; 5}; 4. There are three main points in the singleton mode. First, a class can only have one instance. Second, it must create the instance itself. Third, it must provide the instance to the entire system. From the specific implementation point of view, there are the following three points. 1. The class in singleton mode only provides private constructors. 2. The class definition contains a static private object of the class. 3, yes. This class provides a static common function to create or obtain its own static private object.

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.