Java, C #, and JavaScript single-sample mode

Source: Internet
Author: User

Regardless of a language or model, the idea is the same. what is different is the details of the language.

Java version:

public class Singleton {private volatile static Singleton instance;private Singleton(){};public static Singleton getInstance(){if(instance == null){synchronized (Singleton.class) {if(instance == null){instance = new Singleton();}}}return instance;}}

C:

Public class Singleton {protected Singleton () {} Private Static volatile Singleton instance = NULL; // process of creating a unique instance in lazy mode public static Singleton instance () {If (instance = NULL) // The outer if lock (typeof (Singleton) // synchronize shared resources in multiple threads if (instance = NULL) // inner layer if instance = new Singleton (); Return instance ;}}

JS version:

VaR singletontest = (function () {function single (ARGs) {var ARGs = ARGs | []; this. name = 'singletontest'; this. X = args. X | 6; this. y = args. Y | 7 ;}; var instance; VaR _ static = {Name: 'singletontest', getinstance: function (ARGs) {If (instance === undefined) {instance = new single (ARGs) ;}return instance ;}; return-static ;}) (); // call var singletontest = singletontester. getinstance ({pointx: 5 });

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.