[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