How do objects created following singleton work?

10

I would like to understand how objects created following the singleton design pattern work in javascript.

My main doubts are about the methods and attributes of this object, where and how to create them, and where and how to access them.

I have read some articles even in English but I did not quite understand how to correctly use the singleton.

As an example I have this code:

  

Source: Dofactory - Singleton

var Singleton = (function () {
    var instance;

    function createInstance() {
        var object = new Object("I am the instance");
        return object;
    }

    return {
        getInstance: function () {
            if (!instance) {
                instance = createInstance();
            }
            return instance;
        }
    };
})();

function run() {

    var instance1 = Singleton.getInstance();
    var instance2 = Singleton.getInstance();

    alert("Same instance? " + (instance1 === instance2)); //retorna true 
}

The objects are being instantiated in function run , my question would be, if I want to add a method stop and a status attribute to know if this object is in run or stop , how should I do this following the design pattern singleton ?

Reference:
A Beginner's Guide to Design Patterns
JavaScript Design Patterns: Singleton

asked by anonymous 24.12.2015 / 19:23

3 answers

2

Maybe start looking at memory pointers give a clarified.

See this situation using NEW

var a = new Object('Some Object A');
var b = new Object('Some Object B');

In this code we have two objects created in two different variables. In the memory of your machine, it looks something like this:

Código                   Memória
var a ---------------->  Posição 0000
var b ---------------->  Posição 0001

Dare to be, two copies are created.

If I do the following:

var c = b;

In this case, c === b , because:

Código                   Memória
var a ---------------->  Posição 0000
var b ---------------->  Posição 0001
var c ---------------->  Posição 0001

Note that both b and c point to the same memory area, that is, the same object.

This is a basics of how it works, summarized. Now look at your example:

getInstance: function () {
  if (!instance) {
    instance = createInstance();
  }
  return instance;
}

Here you are doing that if instance does not exist, then create an instance. So, every time you call getInstance , the first time it creates with NEW and in the others it only returns the one that was created the first time.

Putting this in the examples I cited:

var a = Singleton.getInstance();
var b = Singleton.getInstance();
var c = Singleton.getInstance();


Código                   Memória
var a ---------------->  Posição 0000
var b ---------------->  Posição 0000
var c ---------------->  Posição 0000

In short, this happens because only once it is created, and in the other, only a reference to the Singleton instance is returned.

I hope the text has helped !!!

    
25.10.2016 / 18:30
0

In your example, Object would be the object you want to make a singleton. Just replace (since you defined your object), and if you only use Singleton, you will always have only one copy of the object.

You should create your methods run and stop within the definition of this object of yours.

For a review of how this is done, take a look here:

24.12.2015 / 22:29
0

One way to view is as follows.

Imagine that you have a database connection class, and you do not want to create a new object in memory every time you need to access the database, then you use Singleton.

This will ensure that you create multiple objects to do a common thing in various parts of the project.

    
16.03.2016 / 00:16