Introduction to one-piece JavaScript design patterns and javascript Design Patterns

Source: Internet
Author: User

Introduction to one-piece JavaScript design patterns and javascript Design Patterns

Single-piece mode description

1. note: The single-piece mode is an object that has been instantiated during static access. This object can only be accessed through a unique entry, and has been accessed by instances or objects to be instantiated; object-oriented languages such as Java ,. net C # in such a dynamic server language, class operations can be smoothly performed to avoid data confusion caused by parallel operations;

2. Benefits of the single-piece mode:

1>. reduce new operations to avoid accelerating frequent memory operations and occupying memory;
2>. Minimize the object overhead of large systems;
3> As mentioned above, operations of some types can be performed in an accurate order to avoid data exceptions caused by parallel processing;

Of course, all the advantages mentioned above are in the server language. In the weak javascript language, do not tangle so much, because the scripts are all operated on the client side, there is no operation conflict problem; it is equivalent to you alone using the entire server, don't worry, the problem that your data will be operated by others;

Instance source code


Copy codeThe Code is as follows:
Var Singleton = {
Instance: null,
MailSender: function (){
Var self = this;
Self. to = '';
Self. title = '';
Self. content = '';
Self. send = function (){
// Send body
}
},
GetInstance: function (){
If (this. instance = null ){
This. instance = new Singleton. MailSender ();
}
Return this. instance;
}
}

Usage:


Copy codeThe Code is as follows:
Var mail = Singleton. getInstance ();
Mail. to = 'toname # mail.com ';
Mail. title = 'send in single-piece mode ';
Mail. content = 'send content ';

Mail. send ();

When a global framework, such as a rich UI framework such as DWZ, creates a global Singleton, Singleton. instance has a value, and it is unnecessary to create another one;

Of course, if it is written like this, it will be clear, with the server language:


Copy codeThe Code is as follows:
Singleton. getInstance (). to = 'toname # mail.com ';
Singleton. getInstance (). title = 'send in Singleton mode ';
Singleton. getInstance (). content = 'send content ';

Singleton. getInstance (). send ();

Other instance descriptions

Where is the single-piece mode useful? For example, when there is a unified configuration file on an operating server, for example, when a large number of concurrent operations still need to pay attention to first come and then arrive, such as the operation process records of the exchange, etc, can be operated in single-piece mode;

In addition, the single-piece mode:

1. The method above is called laziness.

2. Ele. Me Stick mode:

Copy codeThe Code is as follows:
Var Singleton = {
Instance: new Singleton. MailSender (),
MailSender: function (){
Var self = this;
Self. to = '';
Self. title = '';
Self. content = '';
Self. send = function (){
// Send body
}
},
GetInstance: function (){
Return this. instance;
}
}

The same method is used;

Create a single-piece mode using closures to hide instance objects

1. Code:

Copy codeThe Code is as follows:
Var Singleton = (function (){
Var instance = null;
Function MailSender (){
This. to = '';
This. title = '';
This. content = '';
}
MailSender. prototype. send = function (){
// Send body
}
Return {
GetInstance: function (){
If (instance = null ){
Instance = new MailSender ();
}
Return instance;
}
}
})();

2. Usage:

Copy codeThe Code is as follows:
// Same usage
Var mail = Singleton. getInstance ();
Mail. to = 'toname # mail.com ';
Mail. title = 'Closure-type single-piece mode send ';
Mail. content = 'send content ';

Mail. send ();

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.