diff --git a/src/main/kotlin/ru/otus/patterns/Builder.kt b/src/main/kotlin/ru/otus/patterns/Builder.kt new file mode 100644 index 0000000..3f82af8 --- /dev/null +++ b/src/main/kotlin/ru/otus/patterns/Builder.kt @@ -0,0 +1,34 @@ +package ru.otus.patterns + +class FoodOrder private constructor( + val bread: String? = "Flat bread", + val condiments: String?, + val meat: String?, + val fish: String? +) { + data class Builder( + var bread: String? = null, + var condiments: String? = null, + var meat: String? = null, + var fish: String? = null + ) { + + fun bread(bread: String) = apply { this.bread = bread } + fun condiments(condiments: String) = apply { this.condiments = condiments } + fun meat(meat: String) = apply { this.meat = meat } + fun fish(fish: String) = apply { this.fish = fish } + fun build() = FoodOrder(bread, condiments, meat, fish) + fun randomBuild() = bread(bread ?: "dry") + .condiments(condiments ?: "pepper") + .meat(meat ?: "beef") + .fish(fish ?: "Tilapia") + .build() + } +} + +val foodOrder = FoodOrder.Builder() + .bread("white bread") + .meat("bacon") + .fish("salmon") + .condiments("olive oil") + .build() \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/patterns/Command.kt b/src/main/kotlin/ru/otus/patterns/Command.kt new file mode 100644 index 0000000..5dbc925 --- /dev/null +++ b/src/main/kotlin/ru/otus/patterns/Command.kt @@ -0,0 +1,38 @@ +package ru.otus.patterns + +interface Command { + fun execute() +} + +data class AddCommand(val x: Int, val y: Int) : Command { + override fun execute() { + val result = x + y + println("+($x, $y) => $result") + + } +} + +data class SubCommand(val x: Int, val y: Int) : Command { + override fun execute() { + val result = x - y + println("-($x, $y) => $result") + } +} + +fun executeCommand(command: Command) { + command.execute() +} + +fun runCommands(vararg commands: Command) { + for (command in commands) { + executeCommand(command) + } +} + +fun main() { + runCommands( + AddCommand(1, 2), + SubCommand(4, 7) + ) +} + diff --git a/src/main/kotlin/ru/otus/patterns/Decorator.kt b/src/main/kotlin/ru/otus/patterns/Decorator.kt new file mode 100644 index 0000000..0fb5858 --- /dev/null +++ b/src/main/kotlin/ru/otus/patterns/Decorator.kt @@ -0,0 +1,43 @@ +package ru.otus.patterns + +import kotlin.random.Random + +fun main() { + WithRepeat(PhoneInstance()).makeCall() +} + + +interface Phone { + fun makeCall(): Boolean +} + + +class PhoneInstance() : Phone { + override fun makeCall(): Boolean { + println("Try calling...") + + val success = Random.nextInt(0, 10) + return success == 1 + } +} + + +class WithRepeat(private val phone: PhoneInstance) : Phone { + override fun makeCall(): Boolean { + fun trying() { + val success = phone.makeCall() + + if (!success) { + println("Error Call") + println("Calling a second time") + trying() + } + } + + trying() + + return true + } +} + + diff --git a/src/main/kotlin/ru/otus/patterns/Singleton.kt b/src/main/kotlin/ru/otus/patterns/Singleton.kt new file mode 100644 index 0000000..f36a5f4 --- /dev/null +++ b/src/main/kotlin/ru/otus/patterns/Singleton.kt @@ -0,0 +1,13 @@ +package ru.otus.patterns + +private class Singleton { + companion object { + private var instance: Singleton? = null + fun getInstance(): Singleton { + if (instance == null) { + instance = Singleton() + } + return instance!! + } + } +} \ No newline at end of file