From 08f975755c803d76c7deb981ea6261667da1a576 Mon Sep 17 00:00:00 2001 From: MVSYCHEV Date: Sun, 29 Oct 2023 21:09:37 +0300 Subject: [PATCH] add homework --- src/main/kotlin/Builder.kt | 39 +++++++++++++++++++++++++ src/main/kotlin/Command.kt | 32 +++++++++++++++++++++ src/main/kotlin/Decorator.kt | 55 ++++++++++++++++++++++++++++++++++++ src/main/kotlin/Singleton.kt | 11 ++++++++ 4 files changed, 137 insertions(+) create mode 100644 src/main/kotlin/Builder.kt create mode 100644 src/main/kotlin/Command.kt create mode 100644 src/main/kotlin/Decorator.kt create mode 100644 src/main/kotlin/Singleton.kt diff --git a/src/main/kotlin/Builder.kt b/src/main/kotlin/Builder.kt new file mode 100644 index 0000000..72188d6 --- /dev/null +++ b/src/main/kotlin/Builder.kt @@ -0,0 +1,39 @@ +data class Customer( + val name: String, + val timeVisit: Double, + val queueNumber: Int +) + +class CustomerBuilder() { + private var name = "Какой-то покупатель" + private var timeVisit = 9.00 + private var queueNumber = 0 + + fun setName(name: String): CustomerBuilder { + this.name = name + return this + } + + fun setTimeVisit(timeVisit: Double): CustomerBuilder { + this.timeVisit = timeVisit + return this + } + + fun setQueueNumber(queueNumber: Int): CustomerBuilder { + this.queueNumber = queueNumber + return this + } + + fun build(): Customer { + return Customer( + name, timeVisit, queueNumber + ) + } +} + +fun main() { + val ordinaryCustomer: Customer = CustomerBuilder().build() + val someCustomer: Customer = CustomerBuilder().setName("Жора").setTimeVisit(12.00).setQueueNumber(1).build() + println(ordinaryCustomer.toString()) + println(someCustomer.toString()) +} \ No newline at end of file diff --git a/src/main/kotlin/Command.kt b/src/main/kotlin/Command.kt new file mode 100644 index 0000000..7700762 --- /dev/null +++ b/src/main/kotlin/Command.kt @@ -0,0 +1,32 @@ +interface Command { + fun execute() + +} +class CommandWrapper() { + fun go(command: Command) = command.execute() +} + +class FootballPlayer(): Command { + override fun execute() { + println("Бегу и бью") + } +} + +class BasketballPlayer(): Command { + override fun execute() { + println("Прыгаю и кидаю") + } +} + +class ChessPlayer(): Command { + override fun execute() { + println("Конь Е5, далее слоном шах, далее пешкой мат") + } +} + +fun main() { + val commandWrapper = CommandWrapper() + commandWrapper.go(FootballPlayer()) + commandWrapper.go(BasketballPlayer()) + commandWrapper.go(ChessPlayer()) +} \ No newline at end of file diff --git a/src/main/kotlin/Decorator.kt b/src/main/kotlin/Decorator.kt new file mode 100644 index 0000000..7c42e43 --- /dev/null +++ b/src/main/kotlin/Decorator.kt @@ -0,0 +1,55 @@ +interface Lemonade { + fun getCost(): Int + fun getSugarPercent(): Double + fun getCompound(): String + +} + +class OrdinaryLemonade: Lemonade { + override fun getCost(): Int { + return 10 + } + + override fun getSugarPercent(): Double { + return 10.0 + } + + override fun getCompound(): String { + return "Вода, сахар, джем" + } +} + +abstract class LemonadeDecorator(private val decoratedLemonade: Lemonade): Lemonade { + override fun getCost(): Int { + return decoratedLemonade.getCost() + } + + override fun getSugarPercent(): Double { + return decoratedLemonade.getSugarPercent() + } + + override fun getCompound(): String { + return decoratedLemonade.getCompound() + } +} + +class NoSugarLemonade(decoratedLemonade: Lemonade): LemonadeDecorator(decoratedLemonade) { + override fun getCost(): Int { + return 5 + } + + override fun getSugarPercent(): Double { + return 0.0 + } + + override fun getCompound(): String { + return "Вода, джем" + } +} + +fun main() { + val ordinaryLemonade = OrdinaryLemonade() + val lemonadeWithoutSugar = NoSugarLemonade(ordinaryLemonade) + println("Стоимость - ${ordinaryLemonade.getCost()}, Сахара - ${ordinaryLemonade.getSugarPercent()}, Состав - ${ordinaryLemonade.getCompound()}") + println("Стоимость - ${lemonadeWithoutSugar.getCost()}, Сахара - ${lemonadeWithoutSugar.getSugarPercent()}, Состав - ${lemonadeWithoutSugar.getCompound()}") +} \ No newline at end of file diff --git a/src/main/kotlin/Singleton.kt b/src/main/kotlin/Singleton.kt new file mode 100644 index 0000000..9dfb704 --- /dev/null +++ b/src/main/kotlin/Singleton.kt @@ -0,0 +1,11 @@ +class Singleton private constructor() { + companion object { + val instance: Singleton by lazy { Singleton() } + } + + fun testSingleton() = println("Test Singleton") +} + +fun main() { + Singleton.instance.testSingleton() +} \ No newline at end of file