Detail javascript Design Mode (single interest mode)

Keywords: Javascript Java

I try to use the least words and space to explain all aspects of the design pattern.
Article link

Understanding the simple interest model

Ensure that there is only one instance and provide global access.
For example, store, thread pool, global cache, browser window object in redux.

Upper Code: General inert single interest mode

let getSingle = function(fn) {
    let result = 'initial_single';
    return function() {
        if (result === 'initial_single') {
            result = fn.apply(this, arguments);
        }
        return result;
    }
}

// Testing-----
let Tree = function() {
    console.log('something')
}
let getSingleTree = getSingle(Tree)
getSingleTree() // Output on first call: console.log('something')
getSingleTree() //
getSingleTree() //

// Three calls will only output once

The evolution of simple interest model

1. Ordinary simple interest

Use variables to mark whether an object has been created, and directly return the previously created variables

Upper Code:

let Person = function(name) {
    this.name = name
}
Person.prototype.getName = function() {
    console.log(this.name)
}
Person.getSingle = (function() {
    let instance = null;
    return function(name) {
        if (!instance) {
            instance = new Person(name)
        }
        return instance
    }
})();

2. Transparency and simple interest

There is a class, no matter how many times you new. It returns you the first instance of new. This is the transparent single interest model. The so-called transparency means that you can't see that it is single interest.

Upper Code:

let Person = (function() {
    let instance;
    Person = function(name) {
        if (instance) {
            return instance;
        }
        this.name = name;
        return instance = this;
    }
    return Person;
})();
let p = new Person('C#')
let a = new Person('java')
console.log(p === a) // true

3. Realize simple profit with agent

Before the implementation of simple interest, there was a common problem: the class and the code of simple interest were interwoven together. This violates the principle of "single function".

Agent is to separate what a class should do from what simple interest should do
Upper Code:

// class
var Person = function(name) {
    this.name = name;
}
// agent
let ProxySinglePerson = (function() {
    let result;
    return function(name) {
        if (result) {
            return result
        }
        result = new Person(name)
        return result
    }
})();

let p = new ProxySinglePerson('C#')
let a = new ProxySinglePerson('java')
console.log(p === a) // true

5. Inertia and simple profit

This means that it can only be created when it is needed. This is a very important point in the application of simple interest model.
In fact, the previous code has also included inert simple interest. See the following code. Focus on "inertia".

Upper Code:

// class
var Person = function(name) {
    this.name = name;
}
Person.getSingle = (function() {
    let instance;
    return function(name) {
        if (!instance) {
            instance = new Person(name);
        }
        return instance;
    }
})();
var p = Person.getSingle('C#')
var a = Person.getSingle('java')
console.log(p === a) // true

6. General inert simple interest

Once and for all, this time we have completed a general inert single interest. That's the code at the beginning of the article
let getSingle = function(fn) {
    let result = 'initial_single';
    return function() {
        if (result === 'initial_single') {
            result = fn.apply(this, arguments);
        }
        return result;
    }
}

Summary

This chapter studies the evolution of the simple interest model, and also refers to the agent model and the principle of single responsibility. I will explain them in detail in the following articles.

In getSingle function, the concepts of closure and higher-order function are also mentioned. Singleton mode is a very practical mode, especially the inert singleton technology, which creates objects only when it is appropriate, and is globally unique. What's more wonderful is that the responsibility of creating objects and managing the single interest is distributed in two different methods. Only when the two methods are combined, can the single interest mode have the power.

Posted by dud3r on Sun, 21 Jun 2020 21:54:51 -0700