From c5f0c771a957c358f36a3fea4dd913e8a80ff873 Mon Sep 17 00:00:00 2001 From: Tuged <104727740+Tuged@users.noreply.github.com> Date: Sun, 29 Oct 2023 16:55:02 +0800 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D0=B5?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B0=D1=82=D1=82=D0=B5=D1=80=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Builder.kt | 39 +++++++++++++++++++++++++++++++++++++++ Command.kt | 33 +++++++++++++++++++++++++++++++++ Decorator.kt | 25 +++++++++++++++++++++++++ SingleLazy.kt | 24 ++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 Builder.kt create mode 100644 Command.kt create mode 100644 Decorator.kt create mode 100644 SingleLazy.kt diff --git a/Builder.kt b/Builder.kt new file mode 100644 index 0000000..036a163 --- /dev/null +++ b/Builder.kt @@ -0,0 +1,39 @@ +class Person { + private var name: String? = null + private var surname: String? = null + private var age = 0 + + class Builder { + private val newPerson: Person + fun withName(name: String?): Builder { + newPerson.name = name + return this + } + + fun withSurname(surname: String?): Builder { + newPerson.surname = surname + return this + } + + fun withAge(age: Int): Builder { + newPerson.age = age + return this + } + + fun build(): Person { + return newPerson + } + + init { + newPerson = Person() + } + } +} + +fun main(){ + val myPerson = Person.Builder() + .withName("John") + .withSurname("Connor") + .withAge(39) + .build() +} \ No newline at end of file diff --git a/Command.kt b/Command.kt new file mode 100644 index 0000000..5a788ea --- /dev/null +++ b/Command.kt @@ -0,0 +1,33 @@ +interface Command{ + fun execute() +} +class Lamp{ + fun on(){ + println("Lamp is on") + } + fun off(){ + println("Lamp is off") + } +} +class OnLamp(val lamp: Lamp): Command { + override fun execute() { + lamp.on() + } +} +class OffLamp(val lamp: Lamp): Command { + override fun execute() { + lamp.off() + } +} +class LampInvoker(var command: Command) { + fun execute() { + command.execute() + } +} +fun main(){ + val lamp = Lamp() + val onLamp = OnLamp(lamp) + val offLamp = OffLamp(lamp) + val lampInvoker = LampInvoker(onLamp) + lampInvoker.execute() +} \ No newline at end of file diff --git a/Decorator.kt b/Decorator.kt new file mode 100644 index 0000000..a32e1e9 --- /dev/null +++ b/Decorator.kt @@ -0,0 +1,25 @@ +interface Machine{ + fun drive(){ + } +} + +open class Car: Machine{ + override fun drive() { + println("This is car") + } +} + +class ColoredCar(val sample:Car, val color: String): Car(){ + override fun drive() { + sample.drive() + println("Color of car is $color") + } +} + +fun main() { + val car = Car() + car.drive() + val color = readLine()!!.toString() + val coloredCar = ColoredCar(car,color) + coloredCar.drive() +} \ No newline at end of file diff --git a/SingleLazy.kt b/SingleLazy.kt new file mode 100644 index 0000000..8c813e8 --- /dev/null +++ b/SingleLazy.kt @@ -0,0 +1,24 @@ +object Singleton{ + + init { + println("1") + } + + object Holder { + val INSTANCE = Singleton + } + + object Lazy { + val instance: Singleton by lazy { + Holder.INSTANCE + } + } + + var b: String = "2" +} +fun main() { + println(Singleton.Lazy.instance.b) + Singleton.Lazy.instance.b = "3" + println(Singleton.Lazy.instance.b) + println(Singleton.Lazy.instance.b) +} \ No newline at end of file