单例模式:
饿汉模式:
/**
* Created by Huangxiaoxu on 2014/11/8.
*/
public class Singleton {
private static Singleton instace=new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instace;
}
}
懒汉模式:
/**
* Created by Huangxiaoxu on 2014/11/8.
*/
public class Singleton {
// private static Singleton instace=new Singleton();
//
// private Singleton(){}
//
// public static Singleton getInstance(){
// return instace;
// }
private static Singleton instance=null;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null)
instance = new Singleton();
return instance;
}
}