Single-piece mode description
1. Note: Single-piece mode, is the object that has been instantiated in the static access, which can only be accessed through a unique portal, an object that has already been instantiated or is to be instantiated; in a service-side dynamic language such as Java,. Net C #, it can ensure that the operation of the class is carried out smoothly, Avoid situations in which the data is confused by parallel operations;
2. The benefits of a single-piece model:
1>. Reduce the operation of new, so as not to speed up memory frequent operation, occupy memory;
2>. Minimizing the overhead of large system objects;
3>. It is said above, can ensure that some types of operations have a sequential and accurate operation, so as to avoid parallel processing of data anomalies caused by the phenomenon;
Of course, the benefits mentioned above are said to be in the service-side language. In the weak type of JavaScript language, do not tangle so much, because the script is operating on its own client side, there is no operational conflict problem; the equivalent of the entire server you are using, do not worry, your data will be someone else to operate the problem;
Instance source code
Copy Code code 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;
}
}
How to use:
Copy Code code as follows:
var mail = singleton.getinstance ();
mail.to = ' toname#mail.com ';
Mail.title = ' single-piece mode send ';
mail.content = ' send content ';
Mail.send ();
When some global framework, such as DWZ, such as rich UI Framework, create a global Singleton, singleton.instance have a value, there is no need to create;
Of course, if it is written like this, it will be more clear, with the server language one:
Copy Code code as follows:
Singleton.getinstance () to = ' toname#mail.com ';
Singleton.getinstance (). title = ' Single-piece mode send ';
Singleton.getinstance () content = ' Send contents ';
Singleton.getinstance (). Send ();
Other actual notes
Where is the single piece mode, more useful? For example, there is an operation server on a unified configuration file, such as large-scale concurrent operations also need to pay attention to the situation of arrival, such as the exchange of the operation of the record, can be used in a single piece mode to operate;
In addition: the way of the single piece mode:
1. The way it's called laziness.
2. Hungry Stick Way:
Copy Code code 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;
}
}
Use the same method;
Use closures to create a single piece pattern, hiding instance objects
1. Code:
Copy Code code 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. How to use:
Copy Code code as follows:
Same usage
var mail = singleton.getinstance ();
mail.to = ' toname#mail.com ';
Mail.title = ' Closed-Package single-piece mode sending ';
mail.content = ' send content ';
Mail.send ();