From 6d59e786a2604625d316317c0da3300655d38d35 Mon Sep 17 00:00:00 2001 From: SR Date: Sat, 28 Oct 2023 23:39:05 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D0=B0=D1=82=D1=82=D0=B5=D1=80=D0=BD?= =?UTF-8?q?=D1=8B=20Singleton,=20Decorator,=20Command,=20Builder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/ru/otus/homework/builder.kt | 57 +++++++++++++++ src/main/kotlin/ru/otus/homework/command.kt | 73 +++++++++++++++++++ src/main/kotlin/ru/otus/homework/decorator.kt | 67 +++++++++++++++++ src/main/kotlin/ru/otus/homework/singleton.kt | 23 ++++++ 4 files changed, 220 insertions(+) create mode 100644 src/main/kotlin/ru/otus/homework/builder.kt create mode 100644 src/main/kotlin/ru/otus/homework/command.kt create mode 100644 src/main/kotlin/ru/otus/homework/decorator.kt create mode 100644 src/main/kotlin/ru/otus/homework/singleton.kt diff --git a/src/main/kotlin/ru/otus/homework/builder.kt b/src/main/kotlin/ru/otus/homework/builder.kt new file mode 100644 index 0000000..e5877ca --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/builder.kt @@ -0,0 +1,57 @@ +package ru.otus.homework + +/** + * Паттерн Builder + */ +open class HouseBuilder { + private var floors: Int? = null + private var area: Double? = null + private var painting: Boolean? = null + private var material: String? = null + + fun setFloors(i: Int){ + this.floors=i + } + fun getFloors(): Int?{ + return this.floors + } + fun setArea(d: Double){ + this.area=d + } + fun getArea(): Double?{ + return this.area + } + fun setPainting(b: Boolean){ + this.painting=b + } + fun getPainting(): Boolean? { + return this.painting + } + fun setMaterial(s: String){ + this.material=s + } + fun getMaterial(): String? { + return this.material + } +} + +class House(var houseBuilder: HouseBuilder) { + fun build(): String { + return """Ваш новый дом + Этажей: ${houseBuilder.getFloors()} + Площадь: ${houseBuilder.getArea()} + Покраска: ${houseBuilder.getPainting()} + Материал стен: ${houseBuilder.getMaterial()} + """ + } +} + +fun main() { + val builder = HouseBuilder() + builder.setFloors(2) + builder.setArea(230.4) + builder.setMaterial("кирпич") + + val house = House(builder) + println( house.build() ) +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/command.kt b/src/main/kotlin/ru/otus/homework/command.kt new file mode 100644 index 0000000..f067aac --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/command.kt @@ -0,0 +1,73 @@ +package ru.otus.homework + +/** + * Паттерн Command + */ + +interface Command { + fun execute() +} + +class OpenCommand(val device: Device) : Command { + override fun execute() { + device.unlockDevice() + } +} +class LockAllCommand(val device: ArrayList) : Command { + override fun execute() { + device.forEach { it.lockDevice() } + } +} + + +interface Device { + fun unlockDevice() + fun lockDevice() +} + +class Safe : Device { + override fun unlockDevice() { + println("Сейф открыт") + } + + override fun lockDevice() { + println("Сейф закрыт") + } +} + + +class SmartDoor : Device { + override fun unlockDevice() { + println("Дверь разблокирована") + } + + override fun lockDevice() { + println("Дверь заблокирована") + } +} + +class Switcher(var c:Command) { + fun accept() { + c.execute() + } +} + +fun main() { + val safeDevice : Device = Safe() // сейф + val cmdOpenSafe = OpenCommand(safeDevice) + val switcherSafe = Switcher(cmdOpenSafe) + switcherSafe.accept() // откроем сейф + + val smartDoor : Device = SmartDoor() // умная дверь + val cmdOpenDoor = OpenCommand(smartDoor) + val switcherDoor = Switcher(cmdOpenDoor) + switcherDoor.accept() // откроем дверь + + // все сразу закроем + val allDevices = ArrayList() + allDevices.add(safeDevice) + allDevices.add(smartDoor) + val cmdAllLock = LockAllCommand(allDevices) + val switchAll = Switcher(cmdAllLock) + switchAll.accept() +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/decorator.kt b/src/main/kotlin/ru/otus/homework/decorator.kt new file mode 100644 index 0000000..3144392 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/decorator.kt @@ -0,0 +1,67 @@ +package ru.otus.homework + +/** + * Паттерн Decorator на примере шиномнтажа)) + */ +interface TireService { + fun description(): String + fun cost(): Double +} + +class ChangeTire : TireService { + override fun description(): String { + return "Замена колеса (снять/поставить)" + } + + override fun cost(): Double { + return 1000.0 + } +} + +class RepairTire : TireService { + override fun description(): String { + return "Ремонт прокола шины" + } + + override fun cost(): Double { + return 300.0 + } +} + +abstract class DecoratorAddService(private val service: TireService) : TireService { + abstract override fun description(): String + + abstract override fun cost(): Double +} + +class Difficult(private val service: TireService) : DecoratorAddService(service) { + override fun description(): String { + return service.description() + ", высокая сложность работы" + } + + override fun cost(): Double { + return service.cost() + 500.0 + } +} + +class VipClient(private val service: TireService) : DecoratorAddService(service) { + override fun description(): String { + return service.description() + ", VIP" + } + + override fun cost(): Double { + return service.cost() + 1000.0 + } +} + +fun main() { + println("Шинка СУПЕР") + val changeTire : TireService = ChangeTire() // замена колес + println("${changeTire.description()} = ${changeTire.cost()}") + + var repairTire : TireService = RepairTire() // ремонт колес + repairTire = Difficult(repairTire) // + сложность + repairTire = VipClient(repairTire) // + vip обслуживание + + println("${repairTire.description()} = ${repairTire.cost()}") +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/singleton.kt b/src/main/kotlin/ru/otus/homework/singleton.kt new file mode 100644 index 0000000..123c899 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/singleton.kt @@ -0,0 +1,23 @@ +package ru.otus.homework + +/** + * Паттерн Singleton + */ +class Singleton private constructor() { + + companion object { + private var instance : Singleton? = null + fun getInstance(): Singleton { + if (instance == null) { + instance = Singleton() + } + return instance!! + } + } + +} + +fun main() { + // получим единственный экземпляр объекта + val single = Singleton.getInstance() +} \ No newline at end of file From 762ac877b426e8c5ec29b5d67fb3b6905e508b7a Mon Sep 17 00:00:00 2001 From: SR Date: Sun, 29 Oct 2023 13:54:07 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=20Builder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/ru/otus/homework/builder.kt | 70 +++++++++------------ 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/builder.kt b/src/main/kotlin/ru/otus/homework/builder.kt index e5877ca..61a7e11 100644 --- a/src/main/kotlin/ru/otus/homework/builder.kt +++ b/src/main/kotlin/ru/otus/homework/builder.kt @@ -3,55 +3,43 @@ package ru.otus.homework /** * Паттерн Builder */ -open class HouseBuilder { - private var floors: Int? = null - private var area: Double? = null - private var painting: Boolean? = null - private var material: String? = null - - fun setFloors(i: Int){ - this.floors=i - } - fun getFloors(): Int?{ - return this.floors - } - fun setArea(d: Double){ - this.area=d - } - fun getArea(): Double?{ - return this.area - } - fun setPainting(b: Boolean){ - this.painting=b - } - fun getPainting(): Boolean? { - return this.painting - } - fun setMaterial(s: String){ - this.material=s + +class House( + private var floors: Int, + private var area: Double, + private var painting: Boolean?, + private var material: String? +) { + private constructor(builder: Builder) : this(builder.floors, builder.area, builder.painting, builder.material) + + companion object { + inline fun build(floors: Int, area: Double, otherParam: Builder.() -> Unit) = Builder(floors, area).apply(otherParam).build() } - fun getMaterial(): String? { - return this.material + class Builder(var floors: Int, var area: Double) { + var painting: Boolean? = null + var material: String? = null + + fun build(): House { + return House(this) + } } -} -class House(var houseBuilder: HouseBuilder) { - fun build(): String { + override fun toString(): String { return """Ваш новый дом - Этажей: ${houseBuilder.getFloors()} - Площадь: ${houseBuilder.getArea()} - Покраска: ${houseBuilder.getPainting()} - Материал стен: ${houseBuilder.getMaterial()} + Этажей: ${this.floors} + Площадь: ${this.area} + Покраска: ${this.painting} + Материал стен: ${this.material} """ } } fun main() { - val builder = HouseBuilder() - builder.setFloors(2) - builder.setArea(230.4) - builder.setMaterial("кирпич") - val house = House(builder) - println( house.build() ) + val house1 = House.build(2, 200.0){} + println(house1) + + // вариант создания дома с доп параметрами + val house2 = House.build(1, 150.0) { material="кирпич"; painting=true } + println(house2) } \ No newline at end of file