黑马程序员技术交流社区
标题:
单例设计模式
[打印本页]
作者:
world.net
时间:
2015-6-16 21:40
标题:
单例设计模式
饿汉式
class Single{
private static final Single s=new Single();
private Single(){}
public static Single getInstance(){
return s;
}
}
延迟加载懒汉式
class Single{
private static final Single s=null;
private Single(){}
punlic static Single getInstance(){
if(s==null){
synchronized(Single.class){
if(s==null)
s= new Single();
}
}
return s;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2