Reading Notes in Javascript design mode: Singleton Mode

Source: Internet
Author: User

The Singleton mode belongs to the creation mode among the three modes. In the design pattern book, the intention is described as follows: ensure that a class has only one instance and provide a global access point to it.

In JavaScript design patterns, Singleton has a broader definition: Singleton is an object used to divide namespaces and organize a batch of related methods and attributes, if it can be instantiated, it can only be instantiated once. In this section, we point out another use of the singleton mode in javascript: Division of namespaces.

1. Simple Singleton

In JavaScript, the simplest Singleton is an object literal:

var Singleton = {attrbute1 : true,attribute2 : 10,function1 : function() {},function2 : function() {}};
Use the dot operator to access the attributes or methods of the object:
Singleton.attribute1 = false;var total = Singleton.attribute2 + 5;var result = Singleton.method1();

2. Divide the namespace:

When the attribute or method declaration is inside Singleton, these members are no longer global, but are wrapped inside the object and need to be accessed using the object name. This avoids global variable pollution and overwrites or modifies functions or attributes in other parts of the application.

3. Singleton with Private Members

To create a private member for Singleton, you must use a closure. Example:

1 mynamespace = {};
2
3 mynamespace. Singleton = (function (){
4 // Private Members
5 var privateattribute1 = false;
6 var privateattribute2 = [1, 2, 3];
7
8 function privatemethod1 (){
9 // do something
10}
11
12 function privatemethod2 (){
13 console. Log ('privatemethod2 ');
14 // do other something
15}
16
17 return {
18 // public members
19 publicattribute1: True,
20 publicattribute2: 10,
21
22 publicmethod1: function (){
23 // call private Method
24 privatemethod2 ();
25 },
26
27 publicmethod2: function (){
28
29}
30 };
31 })();
32
33
34 mynamespace. Singleton. publicmethod1 ();

 

 

Advantages of Singleton mode:

The main benefit of the singleton mode is its role in code organization. Organizing related methods and attributes in a singleton that will not be instantiated multiple times makes it easier to call and maintain code. Wrap methods in singleton to prevent them from being mistakenly modified by other programmers, and to prevent global namespaces from being contaminated by a large number of variables.

Disadvantages of Singleton mode:

Because the singleton mode provides a single point of access, it may lead to strong coupling between modules, which is not conducive to unit testing.

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.