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 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 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 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