Java Singleton mode and Java mode

Source: Internet
Author: User

Java Singleton mode and Java mode
I. Concepts

The Singleton mode is a common software design mode. Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance in a class in the system and the instance is easy to access, so as to conveniently control the number of instances and save system resources. If you want to have only one class object in the system, the singleton mode is the best solution. In my opinion, the Singleton is to prevent external objects from being created.

1.1 Concept Analysis

For a Singleton, the above concepts should meet the following conditions:

1. A singleton class can have only one singleton object;

Second, the singleton class must create its own unique instance object;

Third, this instance object can be accessed by the outside world and cannot be created by the outside world.

Ii. Common Singleton Modes

In java, the singleton mode is generally divided into lazy, hungry, and registration, but the registration type is rarely seen, so it is easy to ignore. If I suddenly want to sum up today, I will not notice this even if I search for information online. The following code is pasted and explained in this way.

2.1 hunger examples
Package com. ygh. singleton;/*** hunger type singleton class * @ author night lone cold * @ version 1.1.1 */public class HungerSingleton {// Private the constructor, the external class cannot use the constructor method. The new object private HungerSingleton () {}// creates an object private static final HungerSingleton lazySinleton = new HungerSingleton (); // you can set the instance acquisition method, return the instance to the caller public static HungerSingleton getInstance () {return lazySinleton ;}}

Write a test class to test whether a singleton is implemented:

Package com. ygh. singleton;/*** Test singleton class ** @ author night lone cold * @ version 1.1.1 */public class Test {public static void main (String [] args) {/** the constructor is private and cannot use the following method to create a new object * // HungerSingleton hungerSingleton = new HungerSingleton (); // use the instance acquisition method to obtain the object HungerSingleton h1 = HungerSingleton. getInstance (); HungerSingleton h2 = HungerSingleton. getInstance (); System. out. println (h1 = h2); // true }}

From the above we can see that the two references of this test class are equal, that is to say, the two references point to the same object, which is exactly in line with the singleton mode standard. At this point, the introduction is complete.

2.2 hunger examples
Package com. ygh. singleton;/*** lazy singleton class * @ author night lone cold * @ version 1.1.1 */public class LazySingleton {// Private the constructor, the external class cannot use the constructor method. The new object private LazySingleton () {}// creates an object, not the final private static LazySingleton lazySingleton = null; // you can set the instance acquisition method, return the instance to the caller public static LazySingleton getInstance () {// when the singleton object does not exist, create if (lazySingleton = null) {lazySingleton = new LazySingleton ();} // return lazySingleton ;}}

Test class:

Package com. ygh. singleton;/*** Test singleton class ** @ author night lone cold * @ version 1.1.1 */public class Test {public static void main (String [] args) {/** the constructor is private and cannot use the following method to create a new object * // LazySingleton lazySingleton = new LazySingleton (); // use the instance acquisition method to obtain the object LazySingleton l1 = LazySingleton. getInstance (); LazySingleton l2 = LazySingleton. getInstance (); System. out. println (l1 = l2); // true }}

From the above we can see that the two references of this test class are equal, that is to say, the two references point to the same object, which is exactly in line with the singleton mode standard. Here, the lazy introduction is over.

2.3 difference between lazy and ELE. Me

When there is no object in the lazy way, a singleton object will be created. When there is an object, no object will be created. This is not so easy to understand, however, if you are interested in learning more, you can use breakpoints in eclipse to Test, add the content in the if braces of the LazySingleton class to the breakpoint, and then run it using debug in the Test class, in this way, an object is created for the first time, but no object is created for the second time.

If the expression is hungry, the final keyword is used to create the object. When the caller needs an instance object, the getInstance method can be used to obtain the created instance.

2.4 registration type Singleton type

I am not very familiar with the registration type Singleton category. I have posted a piece of code on the network for your reference. Please study it on your own.

Import java. util. hashMap; import java. util. map;/*** registered Singleton class * @ author Administrator **/public class RegisterSingleton {private static Map <String, RegisterSingleton> map = new HashMap <String, registerSingleton> (); static {RegisterSingleton single = new RegisterSingleton (); map. put (single. getClass (). getName (), single);}/** Default Construction Method for protection */protected RegisterSingleton () {}/** static factory method, return the unique instance */public static RegisterSingleton getInstance (String name) {if (name = null) {name = RegisterSingleton. class. getName (); System. out. println ("name = null" + "---> name =" + name);} if (map. get (name) = null) {try {map. put (name, (RegisterSingleton) Class. forName (name ). newInstance ();} catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace () ;}} return map. get (name);}/** a schematic commercial Method */public String about () {return "Hello, I am RegSingleton. ";} public static void main (String [] args) {RegisterSingleton single3 = RegisterSingleton. getInstance (null); System. out. println (single3.about ());}}

 

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.