Search
๐Ÿ’ก

enum vs sealed

Enum

โ€ข
๊ณ ์ •๋œ ์ƒ์ˆ˜ ์…‹์„ ํ‘œํ˜„ํ•  ๋•Œ
โ€ข
์ž์ฃผ ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ์ œํ•œ๋œ ํฌ๊ธฐ์˜ ์„ ํƒ์„ ํ•  ๋•Œ

Sealed

โ€ข
ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์ด ์ œํ•œ๋˜์–ด ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์˜ ๊ณ„์ธต์„ ๋งŒ๋“ค ๋•Œ
โ€ข
๋” ํ™•์žฅํ•˜๋Š” ๊ฑธ ์ œํ•œํ•˜๊ณ  ๊ด€๋ จ๋œ ํด๋ž˜์Šค๋“ค์ด ํ™•์žฅ์ด ์•ˆ๋˜๋Š” ๋ง‰ํžŒ ์…‹์ด๋ผ๋Š” ๊ฑธ ํ‘œํ˜„ํ•  ๋•Œ
Enum class:
enum class Color { RED, GREEN, BLUE }
Kotlin
๋ณต์‚ฌ
Sealed class:
sealed class Shape { class Circle(val radius: Double) : Shape() class Square(val sideLength: Double) : Shape() }
Kotlin
๋ณต์‚ฌ
โ€ข
๋‘๋“œ๋Ÿฌ์ง„ ์ฐจ์ด์ ์€, enum class ๋Š” ์ž์‹ ๋งŒ์˜ ์œ ๋‹ˆํฌํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜์ง€๋งŒ Sealed class ๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

Properties

enum class DeliveryStatus(val trackingId: String?) { PREPARING(null), DISPATCHED("27211"), DELIVERED("27211"), } sealed class DeliveryStatus class Preparing() : DeliveryStatus() class Dispatched(val trackingId: String) : DeliveryStatus() class Delivered(val trackingId: String, val receiversName: String) : DeliveryStatus()
Kotlin
๋ณต์‚ฌ
โ€ข
์œ ์—ฐํ•จ: Sealed > Enum
โ€ข
์ œํ•œ์„ฑ: Enum > Sealed
โ€ข
Delivered ๋Š” ๋‘ ๊ฐœ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„๋‹ค.

Functions

enum

enum class DeliveryStatus { PREPARING { override fun cancelOrder() = println("Cancelled successfully") }, DISPATCHED { override fun cancelOrder() = println("Delivery rejected") }, DELIVERED { override fun cancelOrder() = println("Return initiated") }; abstract fun cancelOrder() }
Kotlin
๋ณต์‚ฌ
โ€ข
properties ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ๊ฐ์˜ enum value ๋Š” abstract function ์ด ์ง€์ •๋˜๋ฉด ๊ฐ™์€ function ์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
โ€ข
์ฆ‰ override ๋กœ ๊ฐ๊ฐ์˜ enum ์€ ํ•ด๋‹น funciton ์„ ์žฌ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

Sealed

sealed class DeliveryStatus class Preparing : DeliveryStatus() { fun cancelOrder() = println("Cancelled successfully") } class Dispatched : DeliveryStatus() { fun rejectDelivery() = println("Delivery rejected") } class Delivered : DeliveryStatus() { fun returnItem() = println("Return initiated") }
Kotlin
๋ณต์‚ฌ
โ€ข
๊ฐ๊ฐ์ด ๊ณ ์œ ํ•œ function ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
โ€ข
์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ๊ฐ€๋…์„ฑ์žˆ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
class DeliveryManager { fun cancelOrder(status: DeliveryStatus) = when(status) { is Preparing -> status.cancelOrder() is Dispatched -> status.rejectDelivery() is Delivered -> status.returnItem() } }
Kotlin
๋ณต์‚ฌ
์ด๋ ‡๊ฒŒ!
โ€ข
๋ฌผ๋ก  enum ์ฒ˜๋Ÿผ abstract ๋กœ ๊ฐ•์ œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Inheritance

Enum

โ€ข
์ƒ์†, ๊ตฌํ˜„ ๋‹ค ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
โ€ข
Enum ์ด object ์ด๊ธฐ๋•Œ๋ฌธ์—
class LocallyDispatched : DeliveryStatus.DISPATCHED { } // Error
Kotlin
๋ณต์‚ฌ
โ€ข
์œ„์™€ ๊ฐ™์ด ์ƒ์† ๋ฐ›๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅ
โ€ข
๋˜ํ•œ enum ์€ ๋ชฐ๋ž˜(ํ•จ์ถ•์ ์œผ๋กœ) final ์ด๊ธฐ๋•Œ๋ฌธ์— ์Šค์Šค๋กœ ์ƒ์†ํ•  ์ˆ˜๋„ ์—†๋‹ค.
class FoodDeliveryStatus : DeliveryStatus() { } // Error
Kotlin
๋ณต์‚ฌ
โ€ข
extended ์™€ extending ์ด ๋ชจ๋‘ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค!
โ€ข
interface ๋Š” ์ƒ์†ํ•  ์ˆ˜ ์žˆ๊ธด ํ•˜๋‹ค.
open class OrderStatus { } interface Cancellable { } enum class DeliveryStatus : OrderStatus() { } // Error enum class DeliveryStatus : Cancellable { } // OK
Kotlin
๋ณต์‚ฌ

Sealed

โ€ข
์ƒ์† ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ฐ™์€ ๋ชจ๋“ˆ ๋˜๋Š” ํŒŒ์ผ ๋‚ด์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
class LocallyDispatched : Dispatched() { } // OK class PaymentReceived : DeliveryStatus() // OK
Kotlin
๋ณต์‚ฌ
โ€ข
interface ์ƒ์†๋„ ๋ฌผ๋ก  ๊ฐ€๋Šฅํ•˜๋‹ค.

Number of instances

Enum

โ€ข
enum ์ด type ์ด ์•„๋‹ˆ๋ผ object ๋ผ์„œ ์—ฌ๋Ÿฌ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค(์‹ฑ๊ธ€ํ†ค)
// Single instance val dispatched1 = DeliveryStatus.DISPATCHED // OK // Another instance val dispatched2 = DeliveryStatus.DISPATCHED("45234") // Error
Kotlin
๋ณต์‚ฌ

Sealed

โ€ข
sealed ์˜ ์„œ๋ธŒ ํƒ€์ž…์ด type ์ด๊ธฐ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
โ€ข
๋˜ํ•œ ์œ ๋™์ ์œผ๋กœ object ์„ ์–ธํ˜•์„ ์ด์šฉํ•˜์—ฌ ์‹ฑ๊ธ€ํ†ค์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.
sealed class DeliveryStatus object Preparing : DeliveryStatus() class Dispatched(val trackingId: String) : DeliveryStatus() data class Delivered(val receiversName: String) : DeliveryStatus() // Multiple Instances val dispatched1 = Dispatched("27211") // OK val dispatched2 = Dispatched("45234") // OK // Single Instance val preparing1 = Preparing // OK val preparing2 = Preparing() // Error
Kotlin
๋ณต์‚ฌ

Serializable and Comparable

Enum

โ€ข
๋ชจ๋“  enum ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ธ java ์˜ Enum ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ธฐ๋•Œ๋ฌธ์— eqauls, toString, hashCode, Serializable, Comparable ์„ ์ด๋ฏธ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์–ด์„œ ๊ตณ์ด ๋‹ค์‹œ ์žฌ์ •์˜ํ•  ํ•„์š”๋Š” ์—†๋‹ค(Enum ์€ Object ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์—)

Sealed

โ€ข
์ˆ˜๋™์œผ๋กœ ์žฌ์ •์˜ํ•˜๊ฑฐ๋‚˜ data class ๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

Performance

Enum

โ€ข
Enum ์€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ ์ˆ˜๊ฑฐํ•ด๊ฐ€์ง€ ์•Š์œผ๋ฏ€๋กœ ์„œ๋ฒ„๊ฐ€ ๋– ์žˆ๋Š” ๋™์•ˆ ์˜์›ํžˆ ์‚ด์•„์žˆ๋‹ค(๋ ˆ์ด์ง€ ๋กœ๋”ฉ์ด ์•„๋‹Œ ์‹ฑ๊ธ€ํ†ค์ด๋‹ˆ๊นŒ ๋‹น์—ฐํ•˜๊ธด ํ•˜๋‹ค).
โ€ข
์žฅ์ 
โ—ฆ
ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด์ง€๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ง€์ง€ ์•Š์•„์„œ ๊ด€๋ฆฌ ๋น„์šฉ์ด ๋“ค์ง€ ์•Š๋Š”๋‹ค.
โ—ฆ
enum ๋ผ๋ฆฌ์˜ ๋น„๊ต๊ฐ€ ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์ด๋‹ค.
โ€ข
๋‹จ์ 
โ—ฆ
์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ์—๋„ ๋ฉ”๋ชจ๋ฆฌ์— ๋– ์žˆ์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•œ๋‹ค.
โ—ฆ
enum์ด ๋ณ„๋กœ ์—†์„ ๋•Œ์—๋Š”(ํฌ์ŠคํŒ…์—๋Š” 100~200) ๋ณ„๋กœ ์ƒ๊ด€ํ•  ๊ฒŒ ์•„๋‹ˆ์ง€๋งŒ ๋” ๋งŽ๋‹ค๋ฉด ํ•ด๋‹น enum ๋“ค์„ ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
โ€ข
Comparison
โ—ฆ
tableswitch ๋ฅผ ์ด์šฉํ•ด์„œ ๋น ๋ฅด๊ธด ํ•œ๋ฐ under the hood ๋ผ๊ณ  ํ•œ๋‹ค.

Sealed

โ€ข
object ์„ ์–ธ์„ ํ•˜๋ฉด enum ์ด๋ž‘ ๋‹ค๋ฅผ ๋ฐ”๊ฐ€ ์—†๋‹ค.
โ€ข
Comparison
โ—ฆ
instanceof ๋ฅผ ์ด์šฉํ•ด์„œ ๋Š๋ฆฌ๊ธด ํ•œ๋ฐ ์ด๊ฒƒ๋„ under the hood ์ด๋‹ค.
โ—ฆ
์ˆ˜์ฒœ๊ฐœ์˜ ์ƒ์ˆ˜๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฃจํ”„์—์„œ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€, ์ด์™ธ์˜ ๊ฒฝ์šฐ๋ผ๋ฉด ์ฐจ์ด๋Š” ๋ฏธ๋ฏธํ•˜๋‹ค.
โ€ข
๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ์ˆ˜๊ฑฐ๋ผ์„œ ๋”ฑํžˆ ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.
โ€ข
์ˆ˜์ฒœ๊ฐœ์˜ ์ƒ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด sealed class ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€์ ์—์„œ ์ข‹์€ ์„ ํƒ์ด๋‹ค.

๊ฒฐ๋ก 

Enum

โ€ข
๊ณ ์ •๋˜๊ณ (fixed), ์ž˜ ๋ณ€ํ•˜์ง€ ์•Š๋Š”(unchanging) ์…‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด enum ์„ ์‚ฌ์šฉํ•˜์„ธ์š”
โ€ข
์˜ˆ๋ฅผ ๋“ค๋ฉด ์ผ์ฃผ์ผ์˜ ์š”์ผ, card suit(3 diamond ๊ฐ™์€๊ฑฐ) ๋“ฑ๋“ฑ

Sealed

โ€ข
๊ณ„์ธต๊ณผ ๊ด€๋ จ์„ฑ์ด ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ์ œ์–ด ๊ฐ€๋Šฅํ•˜๊ณ (์œ ์—ฐํ•œ) ๋ณต์žกํ•œ ์„œ๋ธŒ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜์„ธ์š”
โ€ข
๋” ์œ ์—ฐํ•˜๊ณ  ๋ณต์žกํ•œ ๊ณ„์ธต์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋…ํ›„๊ฐ

โ€ข
๊ธ€์“ด์ด๋Š” ์ฝ”ํ‹€๋ฆฐ ์‚ฌ์šฉ์ž์ธ ๋“ฏํ•˜๋‹ค.
โ€ข
Sealed ์˜ˆ์ฐฌํ•˜๋Š” ๋Š๋‚Œ์ด ๋“ ๋‹ค.
โ€ข
๊ทธ๋Ÿฐ๋ฐ ํ™•์‹คํžˆ ์ฝ๋‹ค๋ณด๋‹ˆ Sealed ๊ฐ€ ๋” ์ข‹์•„๋ณด์ธ๋‹ค.
โ€ข
์ฝ”ํ‹€๋ฆฐ์—์„œ Enum ์€ ์ž๋ฐ”์˜ ์ž”์žฌ๋ผ๋Š” ๋Š๋‚Œ์ด ์žˆ๋‹ค.
โ€ข
๊ทธ๋ž˜์„œ ์—„๊ฒฉํ•˜๊ฒŒ ํ†ต์ œํ•  ํ•„์š”์„ฑ์ด ์žˆ์ง€ ์•Š๋Š” ์ด์ƒ(์ผ์ฃผ์ผ์˜ ์š”์ผ ๋“ฑ) Sealed ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ฝ”ํ‹€๋ฆฐ์Šค๋Ÿฝ๋‹ค!
โ€ข
Enum ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด Sealed ์˜ object ์„ ์–ธ์œผ๋กœ ๊ฐ€๋Šฅํ•˜๊ธฐ๋•Œ๋ฌธ์ด๋‹ค!