๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Design Pattern] Delegation Pattern

en.wikipedia.org/wiki/Delegation_pattern

 

Delegation pattern

From Wikipedia, the free encyclopedia Jump to navigation Jump to search In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an obj

en.wikipedia.org

 

 

์ƒ์†๊ณผ ๊ฐ™์ด ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ฒด๋ฅผ ๊ตฌ์„ฑํ•˜๋„๋ก ํ•˜๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ๋””์ž์ธ ํŒจํ„ด.

 

๋‘ ๊ฐœ์˜ ๊ฐ์ฒด(์ˆ˜์‹  ๊ฐœ์ฒด, ๋Œ€๋ฆฌ ๊ฐ์ฒด)๊ฐ€ ์žˆ๋‹ค.

์ˆ˜์‹  ๊ฐ์ฒด๊ฐ€ ๋Œ€๋ฆฌ ๊ฐ์ฒด์—๊ฒŒ ์ž‘์—…์„ ์œ„์ž„ํ•œ๋‹ค.

 

์˜ˆ

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

 

Delegation Pattern Example in Java

In this article, we will learn how to use and implement the Delegation Pattern in Java with an example.

www.sourcecodeexamples.net

 

์ด ์ •๋„ ์˜ˆ์ œ๋Š” ๋‚˜๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒ ๋‹ค. 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