[design mode] JavaScript single-piece mode

Source: Internet
Author: User

Single-piece mode description

1. Description: The 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 been instantiated or is being instantiated, and an object-oriented language such as a server-side dynamic language such as Java,. Net C #, which ensures that the class operates smoothly. Avoid the situation that the data is confused by the parallel operation;

2. Benefits of single-piece mode:

1>. Reduce the operation of new, so as not to speed up the memory of frequent operations, memory consumption;

2>. Minimizing the overhead of large system objects;

3>. Is said above, you can ensure that certain types of operations have an accurate sequence of operations to avoid parallel processing caused by data anomalies;

Of course, the above-mentioned benefits are spoken in the service-side language. In JavaScript this weak type of language, do not tangle so much, because the script is in its own client side of the operation, there is no operational conflict, the equivalent of the entire server for you to use, do not worry about your data will be other people to operate the problem;

Example Source

varSingleton ={instance:NULL, MailSender:function() {        varSelf = This; Self.to= ' '; Self.title= ' '; Self.content= ' '; Self.send=function() {             //Send Body}}, getinstance:function() {        if( This. Instance = =NULL) {             This. Instance =NewSingleton.mailsender (); }        return  This. instance; }}

How to use:

var mail == ' toname#mail.com '= ' single-piece mode send' = ' send content '; mail.send ();

When there are some global frameworks, such as a rich UI framework like DWZ, when a global Singleton is created, Singleton.instance has values, and it is unnecessary to create them;

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

Singleton.getinstance (). To = ' toname#mail.com '= ' single-piece mode send' = ' send content '; Singleton.getinstance (). Send ();

Other practical Instructions

Where is the single-piece mode, and what is more useful? For example, there is a unified configuration file on the operation server, such as large-scale concurrent operation also need to pay attention to the situation of first served, such as the exchange of operating process records, etc., can be used in a single-piece mode to operate;

In addition: a single-piece mode method:

1. The kind called lazy way

2. Hungry Stick Way:

var Singleton = {    new Singleton. MailSender (),    function() {      this. to = ';        this. title = ";       this. Content = ';   },   function()        {return  This . instance;   }}    

Using the same method;

[design mode] JavaScript single-piece mode

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.