package com.java.Test;
// 懒汉模式:只有当需要的时候才会被加载
public class LazyMode {
private static LazyMode lazyMode=null;
//实现构造方法的 私有化
private LazyMode(){}
//提供方法得到实例
public static LazyMode getInstance(){
//类名。class 使用当前类作为一个锁对象
if(lazyMode!=null){
synchronized (LazyMode.class) {
if(lazyMode!=null){
lazyMode=new LazyMode();
}
}
}
return lazyMode;
}
}
//饿汉模式:类一加载就产生一个实例
package com.java.Test;
public class HungryMode {
private static final HungryMode hungryMode=new HungryMode();
private HungryMode(){}
public static HungryMode getInstance(){
return hungryMode;
}
}