I have a question regarding how to model the follo...
# announcements
m
I have a question regarding how to model the following. I have a data class representation of all the supplied data.
Copy code
data 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:
Copy code
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.
d
But I also want to let everyone know, in the model, that SpecificActivity IS an Activity
Why 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 needed
k
Would this do the trick?
c
If I understand you correctly, what you're describing is views, something like:
or you could use secondary constructor:
Copy code
data 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)
	)
}
Otherwise if we are to take it exactly as you described, that's impossible.