Small example of a singleton pattern

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacewindowsformsapplication5{classClass1 {Private StaticClass1 minstance; //Thread Helper Objects        Private Static ReadOnly ObjectLockassistant =New Object(); /// <summary>        ///Single Case/// </summary>        /// <returns></returns>         Public StaticClass1 Instance {Get            {                if(Minstance = =NULL)                {                    Lock(lockassistant) {if(Minstance = =NULL) {minstance=NewClass1 (); }                    }                }                returnminstance; }        }         Public intDD () {return 1; }         Public intcc () {return 2; }    }}

Use the following code to call the above class, debugging under your own: You can master the essence of the singleton mode

int a= Class1.Instance.cc () + CLASS1.INSTANCE.DD ();
MessageBox.Show (A.tostring ());

Like to give me a song praise OH

Small example of a singleton pattern

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.