Implementation of several singleton patterns

Source: Internet
Author: User
Tags static class

Since it is required to generate only one instance, we must set the constructor to a private function to prohibit other instances from being created. We can define a static instance and create the instance when needed.

Here are the code for several singleton patterns:

/** * Singleton Mode * * */public class No2 {/** * singleton mode, lazy, thread-safe */public static class singleton{

        Private final static SingleTon INSTANCE = new SingleTon ();
        Private SingleTon () {} public static SingleTon getinstance () {return INSTANCE; }}/** * Singleton mode, a hungry man type, thread insecure */public static class singleton2{private static SingleTon2 INSTA

        NCE = null;
                Private SingleTon2 () {} public static SingleTon2 getinstance () {if (INSTANCE = = null) {
            INSTANCE = new SingleTon2 ();
        } return INSTANCE; }}/** * Singleton mode, a hungry man, thread-safe, multi-threaded environment inefficient * * public static class singleton3{private static Singl

        ETon3 INSTANCE = null; Private SingleTon3 () {} public static synchronized SingleTon3 getinstance () {if (INSTANCE = =
      NULL) {INSTANCE = new SingleTon3 ();      } return INSTANCE; }}/** * Singleton mode, a hungry man type, another lock method */public static class singleton4{private static SingleTon4 INS

        tance = null;
                Private SingleTon4 () {} public static SingleTon4 getinstance () {if (INSTANCE = = null) { Synchronized (Singleton4.class) {if (INSTANCE = = null) {INSTANCE = new
                    SingleTon4 ();
        }}} return INSTANCE; }}/** * Singleton mode, a hungry man type, variant, thread safe */public static class singleton5{private static SingleTon5 in
        stance = NULL;
        static{INSTANCE = new SingleTon5 ();
        } private SingleTon5 () {} public static SingleTon5 getinstance () {return INSTANCE;  }}/** * Singleton mode, lazy, use static inner class, thread safety "recommended" */public static class singleton6{private final Static Class Singletonholder{private static SingleTon6 INSTANCE = new SingleTon6 (); } private SingleTon6 () {} public static SingleTon6 getinstance () {return Singletonho Lder.
        INSTANCE;
        }}/** * Singleton mode, static inner class, using enumeration method, thread safety "recommended" */public enum singleton7{INSTANCE;  public void Whatevermethod () {}}/** * Singleton mode, static inner class, using double check lock, thread safety "recommended" */public static class

        singleton8{private volatile static SingleTon8 INSTANCE = null;
                Private SingleTon8 () {} public static SingleTon8 getinstance () {if (INSTANCE = = null) { Synchronized (Singleton8.class) {if (INSTANCE = = null) {INSTANCE = new SingleTon
                    8 ();
        }}} return INSTANCE; }} public static void Main (string[] args) {System.out.println (singleton.getinstance () = =Singleton.instance);
        System.out.println (singleton2.getinstance () = = Singleton2.getinstance ());
        System.out.println (singleton3.getinstance () = = Singleton3.getinstance ());
        System.out.println (singleton4.getinstance () = = Singleton4.getinstance ());
        System.out.println (singleton5.getinstance () = = Singleton5.getinstance ());
        System.out.println (singleton6.getinstance () = = Singleton6.getinstance ());
        System.out.println (singleton7.instance = = singleton7.instance);
    System.out.println (singleton8.getinstance () = = Singleton8.getinstance ()); }
}
GitHub Source

GitHub on the code please click here

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.