A hungry man mode of single case mode

Source: Internet
Author: User

Package Com.imooc;/** Single case Mode singleton * Application: Some objects need only one is enough. * Function: Ensure that an instance of the entire application has only one * type: A hungry man mode, lazy mode * **/Public class Singleton {//1. Privatize the construction method and do not allow external direct creation of objectsPrivate Singleton () {}//2. Create a unique instance of the class, using the private static adornmentprivate static Singleton instance=NewSingleton (); //3. Provide a method for obtaining an instance, using the public static modifierPublic static Singleton getinstance () {returninstance; }}

Usage patterns

Package Com.imooc;public class Test {    void  main (string[] args) {        Singleton s1=  Singleton.getinstance ();        Singleton s2=singleton.getinstance ();         if (s1==S2)        {            System.out.println ("S1 and sI2 are the same instance");        } Else {                        System.out.println ("S1 and time is not the same instance")        ;     }}}

A hungry man mode of single case mode

Related Article

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.