One of the several javaScript design patterns-the Single Mode

Source: Internet
Author: User

One of the several javaScript design patterns-the Single Mode

JavaScript is a weak type, dynamic, prototype-based language. This language feature makes it very easy,

It is even common to implement some of these modes.

The idea of the standalone mode is to ensure that a specific class has only one instance. This means that when you use the same

Class to create a new object.

In javaScript, there are no classes and only objects. When you create a new object, there are actually no other objects

Similarly, the new object is already a single object. Using the object literal to create a simple object is also a single object

Example.
Var obj = {
Myprop: 'My value'
};
In javaScript, objects are never completely equal, unless they are the same object, even if

A similar object with identical members will not be exactly the same as the first object.

Var obj2 = {
Myprop: 'mu value'
};

Obj = obj2 // false
Obj = obj2 // false

Therefore, we can think that every time we use the object literal to create an object, we are actually creating a monomer,

And does not involve any special syntax.

There are no classes in javaScript, so the definition of the single-stick chewing Word strictly does not make sense. However

In javaScript, you can use constructors to create objects with the new syntax, and sometimes you may need to use this syntax.

. This idea is that when a constructor is used to create multiple objects using the new operator, only

Get a new pointer to the same object.

The following code shows the expected behavior.
Var uni = new Universe ();
Var uni2 = new Universe ();
Uni = uni2; // true

In the preceding example, the uni object is created only when the constructor is called for the first time. During the second and subsequent creation

Returns the same uni object. This is why uni = uni2, because they essentially point to the same object

. So how to implement this mode in javaScript?
The Universe constructor is required to cache the object instance this so that the constructor can be created and

Returns the same object. There are multiple options to achieve this goal:
1. You can use global variables to store the instance. However, this method is not recommended because,

Global variables have some disadvantages. In addition, anyone can override this global variable.

The instance can be cached in the static attribute of the constructor. The functions in javaScript are also objects, so they

You can also have attributes. You can use properties similar to Universe. instance and cache the instance in this property.

Is a good implementation method. The only drawback of this solution is that the instance attribute is publicly accessible.

This attribute may be modified in external code, so that the instance is lost.
For example:
Function Universe (){
// Determine whether an instance exists
If (typeof Universe. instance === 'object '){
Return Universe. instance;
}
// Normal
This. start_time = 0;
This. bang = "Big ";
// Cache
Universe. instance = this;
}
//
Var uni = new Universe ();
Var uni2 = new Universe ();
Uni = uni2; // true

This method is a very direct solution. Its only drawback is that its instance attribute is public, although

However, it is unlikely that other code will accidentally modify this attribute, but this possibility still exists.

3. You can wrap the instance in a closure. This ensures that the private attributes of the instance are not

The disadvantage of code modification outside the constructor is the extra overhead of closure.
For example:
Function Universe (){
// Cache instance
Var instance = this;

// Normal
This. start_time = 0;
This. bang = "big ";

// Rewrite the constructor.
Universe = function (){
Return instance;
};

}

Var uni = new Universe ();
Var uni2 = new Universe ();
Uni = uni2; // true

When the original constructor is called for the first time, it returns this as usual, and then, in the second and third calls

Will execute the rewrite constructor. The rewriting constructor accesses the private instance variable through the closure, and only

This instance is simply returned. If you rewrite the constructor, you will lose all the information added

Its attributes. Under the specific circumstances of this column, any object added to the universe prototype will not exist

Link to the activity of the Instance created by the original implementation.
Another method is to wrap constructors and instances in real-time functions. When the constructor is called for the first time

Create an object, and the like private instance points to this object. After the second call, the constructor only

Return the private variable. With this new implementation method, all the previous code snippets will be tested as expected

Line.
Var Universe;
(Function (){
Var instance;
Universe = function Unvierse (){
If (instance ){
Return instance;
}
Instance = this;
// All functions
This. start_time = 0;
This. bang = "Big ";
};
}());

Related Article

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.