设计模式-单例模式
package test; /** * 饿汉式单列模式 * @author lenovo * */ public class Singleton1 { private static Singleton1 Singleton1=new Singleton1(); private Singleton1(){ } public static Singleton1 getSingleton1(){ return Singleton1; } }
package test; /** * 此单例模式可以应用于多线程 * @author lenovo * */ public class Singleton2 { private static Singleton2 singleton2=null; private Singleton2(){} public static Singleton2 getSingleton2(){ if(singleton2==null){ synchronized (Singleton2.class) { if(singleton2==null){ singleton2=new Singleton2(); } } } return singleton2; } }