Javascript achieves Singleton mode and javascript implementation

Source: Internet
Author: User

Javascript achieves Singleton mode and javascript implementation

The Singleton mode is also called the singleton mode, and more is also called the singleton mode. It is a simple but most commonly used design mode in software design.

Singleton mode:

In the Application Singleton mode, the class that generates the singleton must ensure that only one instance exists. In many cases, the entire system only needs one global object to coordinate the overall behavior of the system. For example, in the configuration file of the entire system, the configuration data has a singleton object for unified reading and modification, when other objects need to configure data, the configuration data is also retrieved through the singleton object, which simplifies Configuration Management in complex environments.

The idea of the singleton mode is that a class can return an object reference (And always the same) and a method to obtain the instance (static method, usually getInstance name ). When we call this method, if the reference held by the class is not empty, the reference is returned. Otherwise, the instance of the class is created, and assign the instance reference value to the reference maintained by the class before returning. At the same time, the constructor of this class is defined as a private method to avoid other functions using this constructor to instantiate objects. Only the static method of this class is used to obtain the unique instance of this class.

Generally, Javascript generates an instance every time a new object is added, and the instance points to a different address. As follows:

(function(){function Person(name){this.name = name;}Person.prototype.work = function(){console.log(this.name + " is working");}var p1 = new Person("Darren");p1.work();var p2 = new Person("Jack");p2.work();}());

In the preceding example, a Person object is generated for each new object.

How to Implement the singleton mode in Javascript?

-- Determine whether an object exists before generating it. If an object exists, the object is generated if the object does not exist.

(function(){var PersonSingleton = (function(){var instance;function init(){return {name: 'Anonymous',work: function(){console.log(this.name + ' working');}};}return {getInstance: function(){if(!instantiated){instantiated = init();}return instantiated;}}})();var p1 = PersonSingleton.getInstance();p1.work();var p2 = PersonSingleton.getInstance();p2.work();}());

In the preceding example, the Instance obtained through the getInstance method of the PsonSIngleton object is the same each time.

I would like to introduce the knowledge about the javascript Singleton mode and hope to help you.

Articles you may be interested in:
  • Javascript Singleton mode DEMO code javascript Object-Oriented Programming
  • Singleton in JavaScript)
  • Two js Singleton Modes
  • Detailed examples of js Singleton Mode
  • Examples of JavaScript Design Patterns in singleton Mode
  • In-depth understanding of the JavaScript series (25): detailed explanation of the singleton mode of the Design Mode
  • Basic usage of JS mode in singleton Mode
  • Simple implementation of javascript Singleton 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.