상수를 활용해야하는 일이 생겨서
enum 클래스와 sealed 클래스 간에 고민이 되는데요..
when 절과 같은 상황에서 쓰려고 합니다..
enum을 쓸바에는 무조건 sealed 클래스가 낫나요..?
----
sealed class (정확히는 sealed interface)
sealed interface BodyPart {
open val name: String
fun getPart() = name
class Chest : BodyPart {
override val name = "가슴"
}
class Back : BodyPart {
override val name = "등"
}
class Leg : BodyPart {
override val name = "하체"
}
class Shoulder : BodyPart {
override val name = "어깨"
}
class Biceps : BodyPart {
override val name = "이두"
}
class Triceps : BodyPart {
override val name = "삼두"
}
class Abs : BodyPart {
override val name = "복근"
}
}
enum class
enum class BodyType(val part: String) {
CHEST("가슴"),
BACK("등"),
LEG("하체"),
SHOULDER("어깨"),
BICEPS("이두"),
TRICEPS("삼두"),
ABS("복근");
fun getPartName() = part
}
대충 이런식으로 구현할건데.. 어느것을 사용하는것이 더 낫나요?
when 같은곳에서 selaed 클래스를 사용하면 알아서 when 절에 타입이 있나 없나 확인해줘서 안정성 측면에서 더 좋다고 듣긴했습니다..
그리고 when 절뿐만아니라 bundle 에 이 데이터 타입을 넣어서 전송도 해줄 예정입니다.
지금은 String으로만 해주고있어요