gpt4 book ai didi

java - Kotlin中如何设计减少冗余类?

转载 作者:行者123 更新时间:2023-11-30 06:26:14 26 4
gpt4 key购买 nike

我正在学习示例“Kotlin for Android Developers(书籍)”,地址:https://github.com/antoniolg/Kotlin-for-Android-Developers

不同的kt文件中有三个类,我认为这三个类是相似的,并且这些多个类使程序变得复杂。

如何重新设计项目框架,让项目更加清晰?

DbClasses.kt

class DayForecast(var map: MutableMap<String, Any?>) {
var _id: Long by map
var date: Long by map
var description: String by map
var high: Int by map
var low: Int by map
var iconUrl: String by map
var cityId: Long by map

constructor(date: Long, description: String, high: Int, low: Int, iconUrl: String, cityId: Long)
: this(HashMap()) {
this.date = date
this.description = description
this.high = high
this.low = low
this.iconUrl = iconUrl
this.cityId = cityId
}
}

tables.kt

object DayForecastTable {
val NAME = "DayForecast"
val ID = "_id"
val DATE = "date"
val DESCRIPTION = "description"
val HIGH = "high"
val LOW = "low"
val ICON_URL = "iconUrl"
val CITY_ID = "cityId"
}

DomainClasses.kt

data class Forecast(
val id: Long,
val date: Long,
val description: String,
val high: Int,
val low: Int,
val iconUrl: String
)

最佳答案

就数据库相关类而言,您可能会考虑使用 ORM 库来注释字段并生成数据库表模式(无需 DayForecastTable),例如房间(https://developer.android.com/training/data-storage/room/index.html)

从技术上讲,您可以在整个应用程序中使用这些类来减少对 DomainClasses 的需求,尽管我建议保留域层类以使域模型独立于数据库。

关于java - Kotlin中如何设计减少冗余类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47152521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com