Michael de Kaste
02/12/2020, 3:16 PMdata class Activity(
val startTime: LocalDate
val endTime: LocalDate
val directTime: Int
val indirectTime: Int
val travelTime: Int
val quantity: Int
)
It's unsure how this class will be used, but at least, this is the absolute truth of the class. Later on, someone says: I want to use a specific predefined version of this activity, a data class that looks like this:
data class SpecificActivity(
val executionTime: LocalDate
val duration: Int
val quantity: Int
)
but somehow backed by the original, hence; executionTime is just startTime, duration is just endTime - startTime in minutes , and quantity is still the same, the other fields are ignored. But I also want to let everyone know, in the model, that SpecificActivity IS an Activity, just not with the represented fields. such that someone can easily say: I want this SpecificActivity, but I want that other version of it (SomeOtherSpecificActivity, that might not have dates, for example.)
I know about backing fields, sealed classes and the like, but none of them really do what I want.Dennis
02/12/2020, 3:40 PMBut I also want to let everyone know, in the model, that SpecificActivity IS an ActivityWhy so? Who has to know? If the classes don't share all the properties, then maybe they are not related in this sense. It's not uncommon to have one class solely for storing data and other classes that derive their data from this. Like a ViewModel which adapts the relevant information and operations you could also just create a new
SpecificActivity
from the given Activity
whenever neededKroppeb
02/12/2020, 4:19 PMCzar
02/12/2020, 5:01 PMCzar
02/12/2020, 5:10 PMdata class SpecificActivity(
val quantity: Int,
val executionTime: LocalDateTime,
val duration: Duration
) : Activity {
constructor(fullActivity: FullActivity) : this(
quantity = fullActivity.quantity,
executionTime = fullActivity.startTime,
duration = Duration.between(fullActivity.startTime, fullActivity.endTime)
)
}
Czar
02/12/2020, 5:13 PM