From 36fb193c232e3fa46b5f7dba21cf0f0424b17da7 Mon Sep 17 00:00:00 2001 From: viktoriurkov Date: Mon, 30 Oct 2023 20:04:03 +0300 Subject: [PATCH 1/4] [HM-2] singleton pattern with lazy init --- src/main/kotlin/main.kt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/kotlin/main.kt diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt new file mode 100644 index 0000000..bd2c807 --- /dev/null +++ b/src/main/kotlin/main.kt @@ -0,0 +1,8 @@ +fun main() { + // singleton with lazy init + val singleton by lazy { + object { + val singletonVariable = 10 + } + } +} \ No newline at end of file From 1c35104c949a36dfd46c56264ea29dd91f122680 Mon Sep 17 00:00:00 2001 From: viktoriurkov Date: Mon, 30 Oct 2023 20:36:42 +0300 Subject: [PATCH 2/4] [HM-2] decorator impl --- src/main/kotlin/decorator.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/kotlin/decorator.kt diff --git a/src/main/kotlin/decorator.kt b/src/main/kotlin/decorator.kt new file mode 100644 index 0000000..b09a40b --- /dev/null +++ b/src/main/kotlin/decorator.kt @@ -0,0 +1,19 @@ +import java.util.WeakHashMap + +interface Calculator { + fun sum(i: Int, j: Int): Int +} + +class BaseCalculator : Calculator { + override fun sum(i: Int, j: Int): Int { + return i + j + } +} + +class FastCalculator(private var calculator: Calculator): Calculator { + override fun sum(i: Int, j: Int): Int { + System.out.println("sum is ${i + j}") // logging for example. Hard to create smthng unique as example ( + return calculator.sum(i, j) + } + +} \ No newline at end of file From 9e62a4b6cbd687f81ed905e4df05faa3176104d1 Mon Sep 17 00:00:00 2001 From: viktoriurkov Date: Mon, 30 Oct 2023 21:08:23 +0300 Subject: [PATCH 3/4] [HM-2] command impl --- src/main/kotlin/command.kt | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/kotlin/command.kt diff --git a/src/main/kotlin/command.kt b/src/main/kotlin/command.kt new file mode 100644 index 0000000..fc1fc15 --- /dev/null +++ b/src/main/kotlin/command.kt @@ -0,0 +1,49 @@ +interface Calculate { + var result: Number + fun exec() +} + +class Sum(var num1: Int, var num2: Int): Calculate { + override var result: Number = 0 + + override fun exec() { + result = num1 + num2 + } +} + +class Multiply(var num1: Double, var num2: Double): Calculate { + override var result: Number = 0 + + override fun exec() { + result = num1 * num2 + } +} + +class CalculatorInvoker { + private var lock = Any() + private var queue = mutableListOf() + + fun calculate(procedure: Calculate) { + queue.add(procedure) + if (queue.size >= 9) { + synchronized(lock) { + exec() + } + } + } + private fun exec() { + queue.forEach {it.exec()} + queue.clear() + } +} + +class Worker() { + var invoker = CalculatorInvoker() + var sum = Sum(1, 7) + var multiply = Multiply(1.0, 9.0) + + fun start() { + invoker.calculate(sum) + invoker.calculate(multiply) + } +} \ No newline at end of file From 1b118a327bec9494c6026ef335fc9017746d294e Mon Sep 17 00:00:00 2001 From: viktoriurkov Date: Mon, 30 Oct 2023 21:35:06 +0300 Subject: [PATCH 4/4] [HM-2] builder impl --- src/main/kotlin/bulder.kt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/kotlin/bulder.kt diff --git a/src/main/kotlin/bulder.kt b/src/main/kotlin/bulder.kt new file mode 100644 index 0000000..68a0224 --- /dev/null +++ b/src/main/kotlin/bulder.kt @@ -0,0 +1,3 @@ +class Human(var name: String = "John", var lastName: String = "Dow", var age: Int = 18) + +// Если использовать билдер как конфиг объекта с дефолт значениями - то вариант более удобный \ No newline at end of file