Java Singleton mode implementation

Source: Internet
Author: User

Java Singleton mode implementation

Singleton mode:

Q1: What is the singleton mode?

A1: The Singleton mode ensures that a class has only one instance. When you want to use this class object, you can only get one common instance. You must create an instance for the singleton class.

Q2: Why is the singleton mode used?

A2: if there is a complex class, we always need to use its object, then we do not always need to new this object, this will consume our memory, so we need to choose the singleton mode.

 

The following is a simple example to illustrate how to implement the singleton mode:

1 // create an object through new in a common class. Different instances will be created each time. 2 public class singletondemo1 {3 // constructor 4 Public singletondemo1 () {} 5 6 // common method 7 public void singlelove () {8 system. out. println ("persist for you! "); 9} 10} 11 12 13 14 15 16 // use the singleton mode class in the first method, create object 17 public class singletondemo2 {18 // The first method to implement Singleton mode by using public attributes 19 // declare a public static attribute, so that only 20 public static final singletondemo2 singletondemo = new singletondemo2 (); 21 22 // declare a private constructor, so that others cannot create a new object 23 private singletondemo2 () {} 24 25 // normal method 26 Public void singlelove () {27 system. out. println ("always persist for you! "); 28} 29} 30 31 32 33 34 35 // use the singleton mode class of the second method, pass. getinstance () is used to create object 36 public class singletondemo3 {37 // second method to implement Singleton mode. The method is used to instantiate 38 // declare a Private Static attribute, make 39 Private Static final singletondemo3 singletondemo = new singletondemo3 (); 40 41 // declare a private constructor, so that others cannot call the new object only once. After the instance exists, 43 private singletondemo3 () {} 44 45 // declare a public static method, you can call this method to obtain the unique instance 46 public static singletondemo3 getinst. Ance () {47 return singletondemo; 48} 49 50 // common method 51 public void singlelove () {52 system. Out. println ("persist for you always! "); 53} 54} 55 56 57 58 59 60 public class test {61 62 // test method 63 public static void main (string [] ARGs) {64 65 // Test 1 66 singletondemo1 singleton11 = new singletondemo1 (); 67 minute (); 68 69 singletondemo1 singleton12 = new singletondemo1 (); 70 singleton12.singlelove (); 71 72 Boolean flag = singleton11.equals (singleton12); 73 system. out. println ("using the equals Method to Determine"); 74 system. out. print Ln ("flag =" + flag at this time); 75 76 if (FLAG) {77 system. out. println ("their memory address is the same, which means they are the same instance and implement the singleton mode! "); 78} else {79 system. out. println ("their memory address is different, it means they are different instances"); 80} 81 82 83 84 // Test 2 85 singletondemo2 singleton21 = singletondemo2.singletondemo; 86 singleton21.singlelove (); 87 88 singletondemo2 singleton22 = pushed; 89 singleton22.singlelove (); 90 91 int addr1 = singleton21.hashcode (); 92 int addr2 = singleton22.hashcode (); 93 system. out. println ("judge by hashcode"); 94 If (addr1 = addr2) {95 system. out. println ("the memory address is the same, it means they are the same instance and realize the singleton mode"); 96} else {97 system. out. println ("their memory address is different, it means they are different instances"); 98} 99 100 101 102 3103 // test singletondemo3 single31 = singletondemo3.getinstance (); 104 single31.singlelove (); 105 106 singletondemo3 single32 = singletondemo3.getinstance (); 107 single32.singlelove (); 108 109 Boolean flag3 = single31.equals (single32); 110 system. out. println ("using equals to judge"); 111 system. out. println ("flag =" + flag3); 112 113 If (flag3) {114 system. out. println ("the memory address is the same, it means they are the same instance and realize the singleton mode"); 115} else {116 system. out. println ("their memory addresses are different, indicating they are different instances"); 117} 118} 119}

 

Note: Based on personal experience, method 3 is recommended.

 

According to comments from netizens, the enumerated Singleton mode is provided here.

1 Public Enum singletontest {2 himself; // defines an enumeration element, which represents an instance of singletontest 3 private string anotherfield; 4 5 singletontest () {6 // the things to be done when singletontest is born. 7 // This method can also be removed. Put what needs to be done during the construction into the instance assignment: 8/** himself = singletontest () {9 * // What singletontest was born: 10 *} 11 */12} 13 14 public void test () {15 system. out. println ("this is a test"); 16} 17} 18 19 20 call: singletontest. himself. test ();

 

 

 

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.