以下是两种简单的使用方法
饿汉模式:
public class TestSingleInstance {
//类一加载就new 出实例,不管你用不用,反正我是饿了,先放在那
private static TestSingleInstance inStance = new TestSingleInstance();
private TestSingleInstance() {
}
public static TestSingleInstance getInstance() {
return inStance;
}
}
懒汉模式:
public class TestSingleInstance {
private static TestSingleInstance inStance = null;
private TestSingleInstance() {
}
public static TestSingleInstance getInstance() {
//啥时用啥时创建,兵来将挡,不用我自己操心
if(inStance == null){
inStance = new TestSingleInstance();
}
return inStance;
}
}