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.