您的位置:首页 > 其它

教你提高电脑的开机速度

2013-08-12 16:21 316 查看
Eager 饿汉模式 : 仅适用于 Java
public class EagerSingleton {
//类被加载时,静态变量就被初始化
private static EagerSingleton ourInstance = new EagerSingleton();

/**
* 外界只能通过此方法获得自身的实例
* @return SingletonDemo
*/
public static EagerSingleton getInstance() {
return ourInstance;
}

/**
* 构造函数对外不可见
* 单例模式最显著的特点
*/
private EagerSingleton() {
}
}

Lazy 懒汉模式 : 适用于Java,C++ (因为static 代码块的执行顺序c++不固定,java是固定的,在构造方法之前)
public class LazySingleton {
//类被加载时,静态变量不会被初始化
private static LazySingleton lazySingleton = null;

/**
* 默认构造函数 是 private
* 防止外界调用,同时此类也不能被继承
*/
private LazySingleton(){

}

/**
* synchronized :同步化
* @return
*/
synchronized public static LazySingleton getInstance(){
if(lazySingleton == null){
lazySingleton = new LazySingleton();
}
return lazySingleton;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: