This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
The singleton pattern
The singleton pattern is defined by ensuring that there is only one instance of a class and providing a global point of access to it.
A Singleton, also known as a Singleton pattern, is a commonly used software design pattern. To apply this pattern, the class of a singleton object must be guaranteed to have only one instance. Many times the entire system only needs to have one global object, which helps us coordinate the overall behavior of the system. For example, in a server program, the configuration information of the server is stored in a file, the configuration data is read by a singleton, and then other objects in the service process through the singleton to obtain the configuration information. This approach simplifies configuration management in complex environments.
The idea behind the singleton pattern is that a class can return a reference to an object (always the same) and a method to get that instance (which must be static, usually using the name getInstance). When we call this method, we return the reference if the reference held by the class is not empty, and if the reference held by the class is empty, we create an instance of the class and assign the instance reference to the reference held by the class; We also define the class constructor as a private method, so that code elsewhere cannot instantiate an object of the class by calling the class constructor, and only gets a unique instance of the class through the static methods provided by the class.
Note: The singleton pattern must be used carefully in multithreaded applications. If two threads call the create method at the same time when the unique instance has not yet been created, they do not detect the existence of the unique instance at the same time and create one instance at the same time, thus two instances are constructed, thus violating the instance unique principle in the singleton pattern. The solution to this problem is to provide a mutex for variables that indicate whether the class has been instantiated (although this is inefficient).
advantages
This pattern can make the script syntax more consistent, and at the bottom of the module code, it can also make it easy for those functions and variables to be publicly accessible, thus improving readability
- 1. In the singleton pattern, there is only one instance of an active singleton, and all instantiations of the singleton class get the same instance. This prevents other objects from instantiating it and ensures that all objects access an instance
- 2. Singleton mode has a certain scalability, the class itself to control the instantiation process, the class in the change of the instantiation process has the corresponding scalability.
- 3. Provides controlled access to a unique instance.
- 4. Since there is only one object in the system memory, system resources can be saved. When objects need to be frequently created and destroyed, singleton mode can undoubtedly improve system performance.
- 5. Allow a variable number of instances.
- 6. Avoid multiple occupation of shared resources.
disadvantages
- 1. Not applicable to changing objects. If objects of the same type always change in different use case scenarios, singletons will cause errors in data and fail to save each other’s state.
- 2. Because there is no abstraction layer in the simple interest pattern, it is very difficult to extend the singleton class.
- 3. Singletons have too much responsibility, which violates the “single responsibility principle” to some extent.
- 4. The abuse of singleton will bring some negative problems. For example, in order to save resources, the database connection pool object is designed as a singleton class, which may lead to too many programs sharing the connection pool object and the connection pool overflow; If an instantiated object is not used for a long time, the system will consider it garbage and collect it, resulting in the loss of the object’s state.
Implement the singleton pattern
Implementing a standard singleton pattern is nothing more than using a variable to indicate whether an object has been created for a class and, if so, returning the previously created object the next time an instance of that class is obtained.
var Singleton = function( name ){ this.name = name; this.instance = null; }; Singleton.prototype.getName = function(){ alert ( this.name ); }; Singleton.getInstance = function( name ){ if ( ! this.instance ){ this.instance = new Singleton( name ); } return this.instance; }; var a = Singleton.getInstance( 'sven1' ); var b = Singleton.getInstance( 'sven2' ); alert ( a === b ); // trueCopy the code
Transparent singleton pattern
Var CreateDiv = (function() {var instance; var CreateDiv = function(html) { if (instance) { return instance; } this.html = html; this.init(); return instance = this; }; CreateDiv.prototype.init = function() { var div = document.createElement('div'); div.innerHTML = this.html; document.body.appendChild(div); } return CreateDiv; }) (); var a = new CreateDiv('seven1'); var b = new CreateDiv('seven2');Copy the code
Introduce an agent to implement the singleton pattern
var CreateDiv = function(html) { this.html = html; this.init(); }; CreateDiv.prototype.init = function() { var div = document.createElement('div'); div.innerHTML = this.html; document.body.appendChild(div); } var ProxySingletonCreateDiv = (function() { var instance; return function(html) { if (! instance) { instance = new CreateDiv(html); } return instance; }}) (); var a = new ProxySingletonCreateDiv('seven1'); var b = new ProxySingletonCreateDiv('seven2');Copy the code
Examples of lazy singletons
Lazy singletons are instances of objects that are created only when needed. Lazy singletons are the focus of the singleton pattern, and this technique is very useful in real world development,
var getSingle=function(fn){
var result;
return function(){
return result||(result = fn.apply(this,arguments))
}
}
Copy the code
That’s all for this post, thank you very much for reading here, if this article is good or a little bit of help to you, please like, follow, share, of course, any questions can be discussed in the comments, I will actively answer, thanks again 😁