代码A使用DBLogHelper.instance
来初始化mDBLogHelper
,代码B使用DBLogHelper()
来初始化mDBLogHelper
。
有什么不同,哪个更好?
代码A
class DBLogHandler(private val mDBLogHelper: DBLogHelper =DBLogHelper.instance) {
}
代码B
class DBLogHandler(private val mDBLogHelper: DBLogHelper =DBLogHelper()) {
}
两者的代码
class DBLogHelper(mContext: Context = UIApp.instance) : ManagedSQLiteOpenHelper(
mContext,
DB_NAME,
null,
DB_VERSION) {
companion object {
val DB_NAME = "log.db"
val DB_VERSION = 1
val instance by lazy { DBLogHelper() }
}
override fun onCreate(db: SQLiteDatabase) {
...
}
}