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..46f59f3 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Builder.kt @@ -0,0 +1,26 @@ +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..e7f735b --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Command.kt @@ -0,0 +1,37 @@ +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..6a8c014 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Decorator.kt @@ -0,0 +1,58 @@ +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..90298a0 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Singleton.kt @@ -0,0 +1,14 @@ +package ru.otus.homework + +class Singleton private constructor() { + + companion object { + val instance:Singleton by lazy { + Singleton() + } + } +} + +fun main() { + val instance = Singleton.instance +}