en.wikipedia.org/wiki/Delegation_pattern
์์๊ณผ ๊ฐ์ด ์ฝ๋ ์ฌ์ฌ์ฉ์ ํ ์ ์๋๋ก ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑํ๋๋ก ํ๋ ๊ฐ์ฒด ์งํฅ ๋์์ธ ํจํด.
๋ ๊ฐ์ ๊ฐ์ฒด(์์ ๊ฐ์ฒด, ๋๋ฆฌ ๊ฐ์ฒด)๊ฐ ์๋ค.
์์ ๊ฐ์ฒด๊ฐ ๋๋ฆฌ ๊ฐ์ฒด์๊ฒ ์์ ์ ์์ํ๋ค.
์
class Rectangle(val width: Int, val height: Int) {
fun area() = width * height
}
class Window(val bounds: Rectangle) {
// Delegation
fun area() = bounds.area()
}
์์ ์ฝ๋
www.sourcecodeexamples.net/2018/05/delegation-pattern.html
์ด ์ ๋ ์์ ๋ ๋๋ ๋ง๋ค ์ ์๊ฒ ๋ค. Kotlin์ผ๋ก ์์ฑ.
interface HandCream {
fun perfume() : String
}
class LoccitaneHandCream : HandCream {
override fun perfume() = "LAVANDER"
}
class KundalHandCream : HandCream {
override fun perfume() = "BABY POWDER"
}
class BouquetGarniHandCream : HandCream {
override fun perfume() = "WHITE MUSK"
}
class HandCreamTest(val handcream : HandCream) : HandCream by handcream {
fun tryIt() {
println(handcream.perfume())
}
}
fun main() {
val loccitaneTest = HandCreamTest(LoccitaneHandCream())
val kundalTest = HandCreamTest(KundalHandCream())
val bouquetGarniTest = HandCreamTest(BouquetGarniHandCream())
loccitaneTest.tryIt()
kundalTest.tryIt()
bouquetGarniTest.tryIt()
}
๊ฒฐ๊ณผ
LAVANDER
BABY POWDER
WHITE MUSK
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] AppCompat Library ์ฌ์ฉ ์ด์ (0) | 2021.03.08 |
---|---|
[Kotlin] Equality ==, === (0) | 2021.03.08 |
[Kotlin] Delegation (0) | 2021.03.05 |
[Programming] Backpressure (0) | 2021.03.02 |
[Kotlin Coroutines] Channels (0) | 2021.03.02 |