From cc99b28cad70ed2e8e1a98025d03611f60a4fc3d Mon Sep 17 00:00:00 2001 From: t-o-n-y-p <42129780+t-o-n-y-p@users.noreply.github.com> Date: Tue, 10 Oct 2023 12:41:28 +0300 Subject: [PATCH 1/5] implemented patterns --- build.gradle | 1 + src/main/kotlin/builder.kt | 15 +++++++++++++++ src/main/kotlin/command.kt | 31 +++++++++++++++++++++++++++++++ src/main/kotlin/decorator.kt | 17 +++++++++++++++++ src/main/kotlin/singleton.kt | 11 +++++++++++ 5 files changed, 75 insertions(+) create mode 100644 src/main/kotlin/builder.kt create mode 100644 src/main/kotlin/command.kt create mode 100644 src/main/kotlin/decorator.kt create mode 100644 src/main/kotlin/singleton.kt diff --git a/build.gradle b/build.gradle index c97e475..834fcd1 100644 --- a/build.gradle +++ b/build.gradle @@ -11,4 +11,5 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib" + implementation "org.jetbrains.kotlin:kotlin-reflect:1.7.21" } \ No newline at end of file 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..7d96c5c --- /dev/null +++ b/src/main/kotlin/command.kt @@ -0,0 +1,31 @@ +interface Command { + fun execute(vararg args: T?) + fun cancel() + fun name(): String +} + +class Shell { + fun launch(c: Command, vararg args: T?) = c.execute(*args) +} + +class SumNumbersCommand : Command { + override fun execute(vararg args: Double?) = println(args.filterNotNull().sum()) + + override fun cancel() = TODO("Not yet implemented") + + override fun name(): String = "sum" +} + +class SortNumbersCommand : Command { + override fun execute(vararg args: Int?) = 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..1185cde --- /dev/null +++ b/src/main/kotlin/decorator.kt @@ -0,0 +1,17 @@ +interface Decorator { + fun printPath() = println( + this::class.annotations + .firstNotNullOfOrNull { it as? Path } + ?.value + ?: "https://otus.ru" + ) + +} + +@Path("https://otus.ru/online") +class Decorated : Decorator + +annotation class Path(val value: String) + + +fun main() = Decorated().printPath() \ 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 From 9e3ece9126a25a9a3d142f0dc1c8364e1aa243ab Mon Sep 17 00:00:00 2001 From: t-o-n-y-p <42129780+t-o-n-y-p@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:36:09 +0300 Subject: [PATCH 2/5] fixed command implementation --- src/main/kotlin/command.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/command.kt b/src/main/kotlin/command.kt index 7d96c5c..24044f9 100644 --- a/src/main/kotlin/command.kt +++ b/src/main/kotlin/command.kt @@ -1,23 +1,23 @@ interface Command { - fun execute(vararg args: T?) + fun execute() fun cancel() fun name(): String } class Shell { - fun launch(c: Command, vararg args: T?) = c.execute(*args) + fun launch(c: Command) = c.execute() } -class SumNumbersCommand : Command { - override fun execute(vararg args: Double?) = println(args.filterNotNull().sum()) +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 : Command { - override fun execute(vararg args: Int?) = println(args.filterNotNull().sorted()) +class SortNumbersCommand(private vararg val args: Int?) : Command { + override fun execute() = println(args.filterNotNull().sorted()) override fun cancel() = TODO("Not yet implemented") @@ -26,6 +26,6 @@ class SortNumbersCommand : Command { fun main() { val shell = Shell() - shell.launch(SumNumbersCommand(), 2.5, 3.5, 8.4) - shell.launch(SortNumbersCommand(), 3, 1, 2) + shell.launch(SumNumbersCommand(2.5, 3.5, 8.4)) + shell.launch(SortNumbersCommand(3, 1, 2)) } \ No newline at end of file From f8ecb7d9c25e6041841e85ea99899b68080c9f98 Mon Sep 17 00:00:00 2001 From: t-o-n-y-p <42129780+t-o-n-y-p@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:42:08 +0300 Subject: [PATCH 3/5] fixed command implementation --- src/main/kotlin/command.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/command.kt b/src/main/kotlin/command.kt index 24044f9..701e82c 100644 --- a/src/main/kotlin/command.kt +++ b/src/main/kotlin/command.kt @@ -1,14 +1,14 @@ -interface Command { +interface Command { fun execute() fun cancel() fun name(): String } class Shell { - fun launch(c: Command) = c.execute() + fun launch(c: Command) = c.execute() } -class SumNumbersCommand(private vararg val args: Double?) : Command { +class SumNumbersCommand(private vararg val args: Double?) : Command { override fun execute() = println(args.filterNotNull().sum()) override fun cancel() = TODO("Not yet implemented") @@ -16,7 +16,7 @@ class SumNumbersCommand(private vararg val args: Double?) : Command { override fun name(): String = "sum" } -class SortNumbersCommand(private vararg val args: Int?) : Command { +class SortNumbersCommand(private vararg val args: Int?) : Command { override fun execute() = println(args.filterNotNull().sorted()) override fun cancel() = TODO("Not yet implemented") From 368f9fe5cd6b78948bb72ead8885c8031f4730b1 Mon Sep 17 00:00:00 2001 From: t-o-n-y-p <42129780+t-o-n-y-p@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:03:44 +0300 Subject: [PATCH 4/5] implemented new decorator --- src/main/kotlin/decorator.kt | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/decorator.kt b/src/main/kotlin/decorator.kt index 1185cde..7448b02 100644 --- a/src/main/kotlin/decorator.kt +++ b/src/main/kotlin/decorator.kt @@ -1,17 +1,27 @@ -interface Decorator { - fun printPath() = println( - this::class.annotations - .firstNotNullOfOrNull { it as? Path } - ?.value - ?: "https://otus.ru" - ) +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 } -@Path("https://otus.ru/online") -class Decorated : Decorator +class Soup(previous: MenuItem? = null) : MenuItem(previous) { + override fun serve(): String = "soup" +} -annotation class Path(val value: String) +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() = Decorated().printPath() \ No newline at end of file +fun main() { + println(Dessert(Entree(Salad(Soup()))).process("Anton")) + println(Dessert(Salad()).process("Maria")) +} \ No newline at end of file From 34dfdc1e591a62e51b00d30b400d6e55bbbb56c5 Mon Sep 17 00:00:00 2001 From: t-o-n-y-p <42129780+t-o-n-y-p@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:05:03 +0300 Subject: [PATCH 5/5] removed reflection lib --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 834fcd1..c97e475 100644 --- a/build.gradle +++ b/build.gradle @@ -11,5 +11,4 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib" - implementation "org.jetbrains.kotlin:kotlin-reflect:1.7.21" } \ No newline at end of file