Single-Case mode

Source: Internet
Author: User

The singleton mode is also known as the list mode, and more is also called the monomer mode. It is a simple but most commonly used design pattern in software design.

The following is an introduction to the single-instance model in Wikipedia:

In the case of Singleton mode, the generation of a singleton class must ensure that only one instance exists, and many times the whole system only needs to have a global object in order to coordinate the behavior of the whole system. For example, in the whole system configuration file, the configuration data has a singleton object for unified reading and modification, other objects need to configure the data also unified through the Singleton object to obtain configuration data, so as to simplify configuration management in a complex environment.

The idea of a singleton pattern is that a class can return a reference to an object (and is always the same) and a method that obtains the instance (a static method, usually using the getinstance name). So when we call this method, if the class holds a reference that returns the reference without being empty, the person creates an instance of the class, and assigns the instance reference to the reference that the class keeps and returns. The constructor of the class is also defined as a private method, so that other functions can use the constructor to instantiate the object, only the static method of the class to get the unique instance of the class.

For JS, great flexibility makes it possible to implement singleton patterns in a variety of ways, using closures to simulate private data, which can be thought of as follows:

  1. var single = (function () {

  2. var unique;

  3. function getinstance () {

  4. if (unique = = = undefined) {

  5. unique = new Construct ();

  6. }

  7. return unique;

  8. }

  9. function Construct () {

  10. // ... Code to generate a single-instance constructor

  11. }

  12. return {

  13. Getinstance:getinstance

  14. }

  15. })();

Above, unique is a reference to the return object, and getinstance is the static method to obtain the instance. Construct is the constructor that creates the instance.

The singleton can be obtained through single.getinstance (), and each invocation gets to the same singleton. This is the effect of the singleton pattern.

However, for JS, it is obvious that the above-mentioned way is too cumbersome, in different scenarios in different ways to achieve the monomer mode is the advantage of JS

Implementation 1: The simplest object literal

    1. var singleton = {

    2. Attr:1,

    3. Method:function () {return this.attr;}

    4. }

    5. var T1 = Singleton;

    6. var t2 = Singleton;

So obviously, T1 = = = t2.

Very simple, and very useful, the disadvantage is that there is no encapsulation, all the property methods are exposed. For some cases where private variables need to be used, it is very weak. Of course, there are some drawbacks to this issue.

Implementation 2: Constructor internal judgment

In fact, the initial JS implementation is a bit similar, but will be placed on the existence of the instance of the class is the decision to put inside the constructor.

    1. function Construct () {

    2. Ensure that only a single case

    3. if (construct.unique!== undefined) {

    4. return construct.unique;

    5. }

    6. Other code

    7. THIS.name = "NYF";

    8. This.age= "24";

    9. Construct.unique = this;

    10. }

    11. var T1 = new Construct ();

    12. var t2 = new Construct ();

Then there are, T1 = = = t2.

is also very simple, nothing but a property to make judgments, but there is no security in this way, once I modified the unique properties of the construct, then the singleton mode is destroyed.

Implementation 3: Closure mode

For the big flexible brand of JS, any problem can find n answers, but let me weigh the pros and cons, the following is a simple way to use closures to implement a single-case pattern, nothing more than a singleton cache will be created.

    1. var single = (function () {

    2. var unique;

    3. function Construct () {

    4. // ... Code to generate a single-instance constructor

    5. }

    6. unique = new Constuct ();

    7. return unique;

    8. })();

Whenever you talk about var t1 = single; var t2 = single; is similar to the object literal method. But the relative safety is a little bit safer, and of course not absolutely.

If you want to use the call single (), then you only need to change the internal return to

return function () {

return unique;

}

The above method can also be done using new method (formalism bright). Of course, this is just a case of closure, you can also determine the existence of a single case in Construct and so on. Various ways can be selected in various situations.

Summarize

In general, the singleton mode is relatively simple in each major mode, but the singleton mode is a more commonly used and useful mode. In JS is particularly prominent (each object literal can be regarded as a single case ~).

Keep in mind that if you strictly need only one instance object's class (although JS does not have the concept of a Class), you should consider using singleton mode.

Using the data cache to store the singleton, as a way to determine whether a single case has been generated, is the primary implementation of the Singleton mode.

Single-Case mode

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.