单例设计模式
解决问题:保证一个类对象在内存中的唯一性。例如:存储了特定的数据文件的配置文件。
class Single{
//静态变量私有化
private static final SINGLE_INSTANCES Single s=new Single();
//构造函数私有化
private single(){
}
//外部程序只能通过方法getInstance()来获取对象
public static getInstance(){
return s;
}
}
//对象延迟加载方法 懒汉式
class Single{
//静态变量私有化
private static SINGLE_INSTANCES Single s=null;
//构造函数私有化
private single(){
}
//外部程序只能通过方法getInstance()来获取对象
public static getInstance(){
if(s==null){
s=new Single();
}
return s;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.