The singleton mode has the following characteristics:
1, the Singleton class can have only one instance.
2, the Singleton class must create its own unique instance.
3. The Singleton class must provide this instance to all other objects
Way one, lazy single case, thread unsafe
public class Singleone {//lazy-slip singleton, thread insecure private static singleone single = Null;private Singleone () {}// Set private to prevent foreign key new an object public static Singleone getinstance () {if (single = null) {System.out.println ("new"); new Singleone ();} return single;}}
Mode two,//a Hungry man type single case, thread safety
public class Singletwo {//A hungry man type singleton, thread safe private static final Singletwo single = new Singletwo ();p rivate singletwo () {}public St atic singletwo getinstance () {System.out.println ("new single"), Return single;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Singleton mode