如何设计减少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类使域模型独立于数据库。
- Android Studio 3.0 Canary 1:引用Kotlin类的Kotlin测试或Java测试失败
- 即使在androidTest包中,Kotlin类也会作为junit测试运行 – 对于Java类,它运行正常
- HashMap如何在Kotlin中实现MutableMap接口?
- 无法在Kotlin中创建Spring Data Event Listener
- 如何创建一个按钮的自定义形状,但保持可点击和可聚焦
- 在Android 4.4上添加Kotlin配置后,DexFile不包含生成的类
- 什么是Kotlin中的Java静态最终字段的等价物?
- 在Kotlin函数类型中表示“超级”泛型?
- 如何用Kotlin interop强制执行泛型