From 2510d977bd7695e11d7237d75a93d7ab448b725f Mon Sep 17 00:00:00 2001 From: mog-hi Date: Fri, 26 Jul 2024 18:55:01 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20proxy=20=ED=8C=A8=ED=84=B4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _21proxy/Printable.kt | 6 ++++++ _21proxy/Printer.kt | 20 ++++++++++++++++++++ _21proxy/PrinterProxy.kt | 26 ++++++++++++++++++++++++++ _21proxy/main.kt | 9 +++++++++ 4 files changed, 61 insertions(+) create mode 100644 _21proxy/Printable.kt create mode 100644 _21proxy/Printer.kt create mode 100644 _21proxy/PrinterProxy.kt create mode 100644 _21proxy/main.kt diff --git a/_21proxy/Printable.kt b/_21proxy/Printable.kt new file mode 100644 index 0000000..af8c596 --- /dev/null +++ b/_21proxy/Printable.kt @@ -0,0 +1,6 @@ +package _21proxy + +interface Printable { + var name: String? + fun print(string: String) +} diff --git a/_21proxy/Printer.kt b/_21proxy/Printer.kt new file mode 100644 index 0000000..8a36332 --- /dev/null +++ b/_21proxy/Printer.kt @@ -0,0 +1,20 @@ +package _21proxy + +class Printer(override var name: String?) : Printable { + init { + heavyJob("Printer 인스턴스 생성 중 name = $name") + } + + override fun print(string: String) { + println("===$name===") + println(string) + } + + private fun heavyJob(message: String) { + println(message) + repeat(5) { + Thread.sleep(1000) + println(".") + } + } +} diff --git a/_21proxy/PrinterProxy.kt b/_21proxy/PrinterProxy.kt new file mode 100644 index 0000000..d48e833 --- /dev/null +++ b/_21proxy/PrinterProxy.kt @@ -0,0 +1,26 @@ +package _21proxy + +class PrinterProxy(initialName: String?) : Printable { + private var real: Printer? = null + + override var name: String? = initialName + @Synchronized + set(value) { + field = value + if (real != null) { + real?.name = value + } + } + + override fun print(string: String) { + realize() + real?.print(string) + } + + @Synchronized + private fun realize() { + if (real == null) { + real = Printer(name) + } + } +} diff --git a/_21proxy/main.kt b/_21proxy/main.kt new file mode 100644 index 0000000..4b2e220 --- /dev/null +++ b/_21proxy/main.kt @@ -0,0 +1,9 @@ +package _21proxy + +fun main() { + val p = PrinterProxy("Alice") + println("이름은 현재 ${p.name} 입니다.") + p.name = "Bob" + println("이름은 현재 ${p.name} 입니다.") + p.print("Hello World!") +}