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