Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 

README.md

κ°œμš”

  • 객체의 ν™•μž₯성을 ν–₯μƒμ‹œν‚€κΈ° μœ„ν•΄μ„œ 주둜 μ‚¬μš©λ˜λŠ” νŒ¨ν„΄μœΌλ‘œ, κ°μ²΄μ—μ„œ λ™μž‘μ„ μ²˜λ¦¬ν•˜λŠ” κ΅¬ν˜„λΆ€μ™€ ν™•μž₯을 μœ„ν•œ 좔상뢀λ₯Ό λΆ„λ¦¬ν•œλ‹€.
  • 즉, κΈ°λŠ₯κ³Ό κ΅¬ν˜„μ— λŒ€ν•΄μ„œ 두 개의 λ³„λ„μ˜ 클래슀둜 κ΅¬ν˜„ν•œλ‹€.
  • (인용) κ΅¬ν˜„(Implemetation) 으둜 λΆ€ν„° 좔상(Abstraction) λ ˆμ΄μ–΄λ₯Ό λΆ„λ¦¬ν•˜μ—¬, 이 λ‘˜μ΄ μ„œλ‘œ λ…λ¦½μ μœΌλ‘œ 관리 될 수 μžˆλ„λ‘ ν•œλ‹€.

예제 μ½”λ“œ

// ν™•μž₯성을 μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€
interface MorseCode {
  fun dot()
  fun dash()
  fun space()
}

// 객체의 λ™μž‘μ„ λŒ€μ‹  μ‹€ν–‰μ‹œμΌœ μ£ΌλŠ” 쀑간 μœ„μž„μž(Bridge)
class MorseCodeDelegate(private val morseCode: MorseCode)  {

  fun dot() {
    morseCode.dot()
  }
  
  fun dash() {
    morseCode.dash()
  }
  
  fun space() {
    morseCode.space()
  }
}

// 1. 점, μ„ , μ—¬λ°±μœΌλ‘œ ν‘œκΈ°ν•˜λŠ” λͺ¨μŠ€μ½”λ“œ κ΅¬ν˜„μ²΄
class DottedMorseCode : MorseCode {

  override fun dot() {
    println(".")
  }
  
  
  override fun dash() {
    println("-")
  }
  
  
  override fun space() {
    println(" ")
  }
}

// 2. μ†Œλ¦¬λ‘œ ν‘œκΈ°ν•˜λŠ” λͺ¨μŠ€μ½”λ“œ κ΅¬ν˜„μ²΄
class SoundMorseCode : MorseCode {

  override fun dot() {
    println("μ‚£!")
  }
  
  
  override fun dash() {
    println("삐~~")
  }
  
  
  override fun space() {
    println("음")
  }
}

// 3. λΉ›μœΌλ‘œ ν‘œκΈ°ν•˜λŠ” λͺ¨μŠ€μ½”λ“œ κ΅¬ν˜„μ²΄
class FlashMorseCode : MorseCode {

  override fun dot() {
    println("번쩍!")
  }
  
  
  override fun dash() {
    println("반짝!!")
  }
  
  
  override fun space() {
    println(" - ")
  }
}

fun main() {
  val morseCode1 = MorseCodeDelegate(morseCode = DottedMorseCode()) // 점,μ„ ,μ—¬λ°±
  morseCode1.dot()
  morseCode1.dash()
  morseCode1.space()
  
  val morseCode2 = MorseCodeDelegate(morseCode = SoundMorseCode())  // μ†Œλ¦¬
  morseCode2.dot()
  morseCode2.dash()
  morseCode2.space()
  
  val morseCode3 = MorseCodeDelegate(morseCode = FlashMorseCode())  // λΉ›
  morseCode3.dot()
  morseCode3.dash()
  morseCode3.space()
}