diff --git a/src/main/kotlin/builder.kt b/src/main/kotlin/builder.kt new file mode 100644 index 0000000..931bc6f --- /dev/null +++ b/src/main/kotlin/builder.kt @@ -0,0 +1,15 @@ +data class Person(val firstName: String, val lastName: String) + +class PersonBuilder { + + private lateinit var firstName: String + private lateinit var lastName: String + + fun firstName(value: String) = this.also { firstName = value } + + fun lastName(value: String) = this.also { lastName = value } + + fun build() = Person(firstName, lastName) +} + +fun main() = println(PersonBuilder().firstName("John").lastName("Smith").build()) \ No newline at end of file diff --git a/src/main/kotlin/command.kt b/src/main/kotlin/command.kt new file mode 100644 index 0000000..701e82c --- /dev/null +++ b/src/main/kotlin/command.kt @@ -0,0 +1,31 @@ +interface Command { + fun execute() + fun cancel() + fun name(): String +} + +class Shell { + fun launch(c: Command) = c.execute() +} + +class SumNumbersCommand(private vararg val args: Double?) : Command { + override fun execute() = println(args.filterNotNull().sum()) + + override fun cancel() = TODO("Not yet implemented") + + override fun name(): String = "sum" +} + +class SortNumbersCommand(private vararg val args: Int?) : Command { + override fun execute() = println(args.filterNotNull().sorted()) + + override fun cancel() = TODO("Not yet implemented") + + override fun name(): String = "sort" +} + +fun main() { + val shell = Shell() + shell.launch(SumNumbersCommand(2.5, 3.5, 8.4)) + shell.launch(SortNumbersCommand(3, 1, 2)) +} \ No newline at end of file diff --git a/src/main/kotlin/decorator.kt b/src/main/kotlin/decorator.kt new file mode 100644 index 0000000..7448b02 --- /dev/null +++ b/src/main/kotlin/decorator.kt @@ -0,0 +1,27 @@ +abstract class MenuItem(private val previous: MenuItem?) { + fun process(name: String): String = + "${previous?.let { "${it.process(name)}, " } ?: "$name's dinner: "}${serve()}" + + protected abstract fun serve(): String +} + +class Soup(previous: MenuItem? = null) : MenuItem(previous) { + override fun serve(): String = "soup" +} + +class Salad(previous: MenuItem? = null) : MenuItem(previous) { + override fun serve(): String = "salad" +} + +class Entree(previous: MenuItem? = null) : MenuItem(previous) { + override fun serve(): String = "entree" +} + +class Dessert(previous: MenuItem? = null) : MenuItem(previous) { + override fun serve(): String = "dessert" +} + +fun main() { + println(Dessert(Entree(Salad(Soup()))).process("Anton")) + println(Dessert(Salad()).process("Maria")) +} \ No newline at end of file diff --git a/src/main/kotlin/singleton.kt b/src/main/kotlin/singleton.kt new file mode 100644 index 0000000..82863c2 --- /dev/null +++ b/src/main/kotlin/singleton.kt @@ -0,0 +1,11 @@ +class Singleton private constructor() { + + fun sayHello() = println("Hello from singleton") + + companion object { + val INSTANCE by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { Singleton() } + } + +} + +fun main() = Singleton.INSTANCE.sayHello() \ No newline at end of file