获取一个数据库管理工具的实例:如果多处使用同一个对象,推荐这样获取实例,防止获得的对象为空或者对象不一致
WeatherDB weatherDB = WeatherDB.getInstance(this);
/**
* 获取weatherDB实例
*/
public synchronized static WeatherDB getInstance(Context context){
if (weatherDB==null) {
weatherDB=new WeatherDB(context);
}
return weatherDB;
}

本文介绍了一种在软件开发中确保数据库管理工具实例唯一性的方法。通过同步静态方法实现单例模式,避免了多处使用同一对象时可能出现的问题。

被折叠的 条评论
为什么被折叠?



