Java single case pattern--Static inner class implementation __java

Source: Internet
Author: User

Singleton is one of the most easily understood in many design patterns, and it is one of the most important design patterns in many design patterns.

The focus of the Singleton mode implementation is to privatize the constructor (private) and to get static private members defined in the class by providing a static public function (publicly synchronized static XXX getinstance) (private static XXX instance, a simple decision to determine whether a static instance is null to control this class can only be new once, that is, control a class can only have a single instance, the general implementation of the following code is shown:

package Com.study.dp.singleton; /** * Traditional way to implement single case mode * @author crazypig * * */public class Traditionalsingletion {//Single instance private static Traditionalsin
	
	Gletion instance;
	Do not let new private traditionalsingletion () {System.out.println ("Private traditionalsingletion ()"); Public synchronized static Traditionalsingletion getinstance () {System.out.println ("Traditionalsingletion Getinsta
		NCE () ");
			if (instance = = null) {SYSTEM.OUT.PRINTLN ("instance = new Traditionalsingletion ()");
		Instance = new Traditionalsingletion ();
	return instance;
		public static void Main (string[] args) {Traditionalsingletion instance = traditionalsingletion.getinstance ();
		System.out.println ("========================================");
		Traditionalsingletion instance01 = Traditionalsingletion.getinstance ();
		System.out.println ("========================================");
	Traditionalsingletion instance02 = Traditionalsingletion.getinstance (); }

}

In an article today, I saw another ingenious way to implement a singleton by defining the reference variable of this single instance in a static inner class, so that it can be judged without the if condition and is multithreaded (guaranteed by the JVM). This particular practice is shown below:

package Com.study.dp.singleton; /** * Static inner class implementation single case mode * @author crazypig * * */public class Specialsingleton {//Static internal class private static classes Nestclass
		{private static Specialsingleton instance;
			static {System.out.println ("instance = new Singletontest ()");
		Instance = new Specialsingleton ();
	}///cannot direct new private Specialsingleton () {System.out.println ("Private Singletontest ()");
		public static Specialsingleton getinstance () {System.out.println ("Singletontest getinstance ()");
	return nestclass.instance;
		public static void Main (string[] args) {Specialsingleton instance = specialsingleton.getinstance ();
		System.out.println ("========================================");
		Specialsingleton instance01 = Specialsingleton.getinstance ();
		System.out.println ("========================================");
	Specialsingleton instance02 = Specialsingleton.getinstance (); }

}
When the getinstance () method is first referenced, the static member variable in the static inner class is accessed, at which point the inner class needs to invoke the static code block (because the class was first accessed). Then accessing the getinstance () method again will directly return the Instace reference. This approach is more ingenious than traditional practice.

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.