Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/kotlin/ru/otus/patterns/Builder.kt
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions src/main/kotlin/ru/otus/patterns/Command.kt
Original file line number Diff line number Diff line change
@@ -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)
)
}

43 changes: 43 additions & 0 deletions src/main/kotlin/ru/otus/patterns/Decorator.kt
Original file line number Diff line number Diff line change
@@ -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
}
}


13 changes: 13 additions & 0 deletions src/main/kotlin/ru/otus/patterns/Singleton.kt
Original file line number Diff line number Diff line change
@@ -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!!
}
}
}