```sealed class Article { abstract val title: ...
# codereview
c
Copy code
sealed class Article {
    abstract val title: String
    abstract val body: String
    abstract val updatedAt: Date
    abstract val categories: List<Category>

    data class New(
        override val title: String,
        override val body: String,
        override val updatedAt: Date,
        override val categories: List<Category>
    ) : Article()

    data class Existing(
        override val title: String,
        override val body: String,
        override val updatedAt: Date,
        override val categories: List<Category>,
        val id: UUID
    ) : Article()
}
👋🏻 I’m working on modeling the different states an object could be in (say for
GET
,
POST
,
PUT
, and
DELETE
) and I’ve landed on this for now. The
New
variant would be exclusively for
POST
requests while the
Existing
would be used for
GET
,
PUT
, and
DELETE
, although the delete would only require the id. Thoughts?
j
I'm still a noob at Kotlin, but I guess the first question I'd ask is have you found any advantage of this over a Decorator pattern? https://medium.com/@HugoMatilla/kotlin-patterns-decorator-pattern-with-class-delegates-982d43305681
c
I don’t think I’d gte much benefit with the decorator pattern in this sense, or at least not through using delegation. I want to keep the hierarchy in place because an Article is an Article, just some sub-types may have more information than others. I am starting to question if I should add an interface for the different states though… The pattern I’m seeing at least with
GET
,
POST
, and
PUT
is with
GET
you have the full picture of the model,
POST
you have the least clear picture since you may delegate the id generation away, similar to
PUT
where you’d know the id but probably don’t want to allow the caller to say when it was updated. So I am starting to look at having an interface for IDs where they would just be a UUID and then similar to date captures. But still not sure… Modeling these requests is trickier than I thought it would be 😂
👍 1
🤣 1
That said, class delegation is very nice. In a way,
val
and
var
are delegates in their own right since they are generating the setters/getters for you
🙌 1