Android 设计模式

来源:互联网 发布:中世纪的生活 知乎 编辑:程序博客网 时间:2024/06/16 13:27
class Instance{
private Instance(){}

public static Instance getSelf(){
return new Instance();
}
}
//懒汉式
class LSingle{
private static  Instance _instance = null; 
private LSingle(){}

public static Instance getInstance(){
if(_instance==null){
synchronized(LSingle.class){
_instance = Instance.getSelf();
}
}
return _instance;
}
}
//饿汉式
class ESingle{
private static Instance _instance = Instance.getSelf();

private ESingle(){}

public static Instance getInstance(){
return _instance;
}

}

懒汉式:在第一次加载这个类的时候创建出这个对象。

饿汉式:在程序加载的时候创建出这个对象。

一般我们使用的懒汉式比较多,因为我们不知道是否能够用到这个对象。



0 0
原创粉丝点击