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

我在https://github.com/antoniolg/Kotlin-for-Android-Developers学习样本“Android开发者Kotlin(书)”

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

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

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的需求),例如Room( https://developer.android.com/training/data-storage/room/ index.html )

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