Singleton mode, Mode

Source: Internet
Author: User

Singleton mode, Mode

1. What is a Singleton?

1. Private Static global variables. The variable type is the type of the class.

2. constructor privatization (avoid using new to create objects)

3. provides public static methods for obtaining such objects.

Ii. What issues does the singleton solve?

Ensure the uniqueness of the Class Object (that is, the class can only have one object)

Iii. Types and code implementation of Singleton

Hunger Model

1 public class Singleton {2 3 private static Singleton instance = null; // static global variable 4 5/** 6 private constructor 7 @ param No 8 */9 private Singleton () {10 11} 12 13 // static initialization Block 14 static {15 instance = new Singleton (); 16} 17 18/*** 19 static method getInstance () 20 @ param No 21 */22 public static Singleton getInstance () {23 // full Han mode 24/* if (instance = null) {25 instance = new Singleton (); 26} */27 return instance; 28} 29}

Test Singleton Mode

1 public class SingletonTest {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 8 // The object cannot be created through new, because the constructor is private 9 // Student s3 = new Student (); 10 // call the static method of the Student class to obtain the object address 11 Singleton s1 = Singleton. getInstance (); 12 Singleton s2 = Singleton. getInstance (); 13/* 14 compares the values of the two variables. If they are equal, only one object is created, 15. Both variables share the object address 16 */17 System. out. println (s1 = s2); 18} 19 20}
View Code

 

Upgrade

 1 public class Singleton1 { 2  3     private static final class SingletonHolder{ 4         private static final Singleton1 INSTANCE = new Singleton1(); 5     } 6      7     private Singleton1(){ 8          9     }10     11     public static Singleton1 getInstance(){12         return SingletonHolder.INSTANCE;13     }14 }

Deserialization version

1 public class Singleton2 implements Serializable {2 3 // prevent creating multiple objects 4 private static final class SingletonHolder {5 private static final Singleton2 INSTANCE = new Singleton2 (); 6} 7 8 private Singleton2 () {9 10} 11 12 public static Singleton2 getInstance () {13 return SingletonHolder. INSTANCE; 14} 15 16 // deserialization 17 private Object readResolve () {18 return SingletonHolder. INSTANCE; 19} 20} 21 22 // enumeration implement Singleton 23/* 24 public enumSingleton {25 INSTANCE; 26 public File openFile (String fileName) {27 return getFile (fileName ); 28} 29 }*/

Full Chinese Model

 1 public class Singleton3 { 2  3     private static volatile Singleton3 singleton3; 4      5     private Singleton3(){ 6          7     } 8      9     public static Singleton3 getInstance(){10         if(null == singleton3){11             synchronized (Singleton3.class) {12                 if(null == singleton3){13                     singleton3 = new Singleton3();14                 }15             }16         }17         return singleton3;18     }19     20 }

 

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.