From aed0f24c3e021d8bcaf9374b35ddf44027fda904 Mon Sep 17 00:00:00 2001 From: LexAndro3723 Date: Fri, 12 May 2023 12:53:37 +0300 Subject: [PATCH 1/2] HomeWork_02 --- src/main/kotlin/ru/otus/homework/Builder.kt | 27 +++++++++ src/main/kotlin/ru/otus/homework/Command.kt | 38 ++++++++++++ src/main/kotlin/ru/otus/homework/Decorator.kt | 59 +++++++++++++++++++ src/main/kotlin/ru/otus/homework/Singleton.kt | 15 +++++ 4 files changed, 139 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..6832dfd --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Builder.kt @@ -0,0 +1,27 @@ +package ru.otus.homework + +class Car( + val model: String?, + val color: String?, + val type: String?) { + + data class Builder( + var model: String? = null, + var color: String? = null, + var type: String? = null) { + + fun model(model: String) = apply { this.model = model } + fun color(color: String) = apply { this.color = color } + fun type(type: String) = apply { this.type = type } + fun build() = Car(model, color, type) + } +} + + +fun main() { + val car = Car.Builder() + .model("Ford Focus") + .color("Black") + .type("Type") + .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..2d65957 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Command.kt @@ -0,0 +1,38 @@ +package ru.otus.homework + +interface OrderCommand { + fun execute() +} + +class OrderAddCommand(private val id: Long) : OrderCommand { + override fun execute() = println("Adding order with id: $id") +} + +class OrderPayCommand(private val id: Long) : OrderCommand { + override fun execute() = println("Paying for order with id: $id") +} + +class CommandProcessor { + + private val queue = ArrayList() + + fun addToQueue(orderCommand: OrderCommand): CommandProcessor = + apply { + queue.add(orderCommand) + } + + fun processCommands(): CommandProcessor = + apply { + queue.forEach { it.execute() } + queue.clear() + } +} + +fun main() { + CommandProcessor() + .addToQueue(OrderAddCommand(1L)) + .addToQueue(OrderAddCommand(2L)) + .addToQueue(OrderPayCommand(2L)) + .addToQueue(OrderPayCommand(1L)) + .processCommands() + } \ 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..cb9aba5 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Decorator.kt @@ -0,0 +1,59 @@ +package ru.otus.homework + +interface ChristmasTree { + fun decorate(): String +} + +class PineChristmasTree : ChristmasTree { + + override fun decorate() = "Christmas tree" +} + +abstract class TreeDecorator + (private val tree: ChristmasTree) : ChristmasTree { + + override fun decorate(): String { + return tree.decorate() + } +} + +class BubbleLights(tree: ChristmasTree) : TreeDecorator(tree) { + + override fun decorate(): String { + return super.decorate() + decorateWithBubbleLights() + } + + private fun decorateWithBubbleLights(): String { + return " with Bubble Lights" + } +} + +fun christmasTreeWithBubbleLights() { + + val christmasTree = BubbleLights(PineChristmasTree()) + val decoratedChristmasTree = christmasTree.decorate() + println(decoratedChristmasTree) +} + +fun christmasTreeWithGarlands() { + + val christmasTree = Garlands(PineChristmasTree()) + val decoratedChristmasTree = christmasTree.decorate() + println(decoratedChristmasTree) +} + +class Garlands(private val tree: ChristmasTree) : ChristmasTree by tree { + + override fun decorate(): String { + return tree.decorate() + decorateWithGarlands() + } + + private fun decorateWithGarlands(): String { + return " with Garlands" + } +} + +fun main() { + christmasTreeWithBubbleLights() + christmasTreeWithGarlands() +} \ 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..f3341c1 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Singleton.kt @@ -0,0 +1,15 @@ +package ru.otus.homework + +class Singleton private constructor() { + + companion object { + val instance:Singleton by lazy { + Singleton() + } + } + +} + +fun main() { + val instance = Singleton.instance +} From 321db31674074652f9e544289590a8e7c6c1e176 Mon Sep 17 00:00:00 2001 From: LexAndro3723 Date: Fri, 12 May 2023 12:58:52 +0300 Subject: [PATCH 2/2] HomeWork_02 --- src/main/kotlin/ru/otus/homework/Builder.kt | 1 - src/main/kotlin/ru/otus/homework/Command.kt | 1 - src/main/kotlin/ru/otus/homework/Decorator.kt | 1 - src/main/kotlin/ru/otus/homework/Singleton.kt | 1 - 4 files changed, 4 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/Builder.kt b/src/main/kotlin/ru/otus/homework/Builder.kt index 6832dfd..46f59f3 100644 --- a/src/main/kotlin/ru/otus/homework/Builder.kt +++ b/src/main/kotlin/ru/otus/homework/Builder.kt @@ -17,7 +17,6 @@ class Car( } } - fun main() { val car = Car.Builder() .model("Ford Focus") diff --git a/src/main/kotlin/ru/otus/homework/Command.kt b/src/main/kotlin/ru/otus/homework/Command.kt index 2d65957..e7f735b 100644 --- a/src/main/kotlin/ru/otus/homework/Command.kt +++ b/src/main/kotlin/ru/otus/homework/Command.kt @@ -15,7 +15,6 @@ class OrderPayCommand(private val id: Long) : OrderCommand { class CommandProcessor { private val queue = ArrayList() - fun addToQueue(orderCommand: OrderCommand): CommandProcessor = apply { queue.add(orderCommand) diff --git a/src/main/kotlin/ru/otus/homework/Decorator.kt b/src/main/kotlin/ru/otus/homework/Decorator.kt index cb9aba5..6a8c014 100644 --- a/src/main/kotlin/ru/otus/homework/Decorator.kt +++ b/src/main/kotlin/ru/otus/homework/Decorator.kt @@ -18,7 +18,6 @@ abstract class TreeDecorator } class BubbleLights(tree: ChristmasTree) : TreeDecorator(tree) { - override fun decorate(): String { return super.decorate() + decorateWithBubbleLights() } diff --git a/src/main/kotlin/ru/otus/homework/Singleton.kt b/src/main/kotlin/ru/otus/homework/Singleton.kt index f3341c1..90298a0 100644 --- a/src/main/kotlin/ru/otus/homework/Singleton.kt +++ b/src/main/kotlin/ru/otus/homework/Singleton.kt @@ -7,7 +7,6 @@ class Singleton private constructor() { Singleton() } } - } fun main() {