黑马程序员技术交流社区
标题:
单例设计模式
[打印本页]
作者:
等你的季节-夏天
时间:
2015-7-3 13:32
标题:
单例设计模式
实现一个类的单例设计模式三步
1. 私有修饰构造方法
2. 自己类成员位置,创建自己类对象
3. 提供公共方法,访问变量
饿汉式:
public class Single {
private Single(){}
private static Single s = new Single();
public static Single getInstance(){
return s;
}
}
懒汉式:
public class Single {
private Single(){}
private static Single s = null;
public static Single getInstance(){
if(s == null){
synchronized(Single.class){
if( s == null ){
s = new Single();
}
}
}
return s;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2