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
26 changes: 26 additions & 0 deletions src/main/kotlin/ru/otus/homework/Builder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.otus.homework

class Car(
val model: String?,
val color: String?,
val type: String?) {

data class Builder(
var model: String? = null,
var color: String? = null,
var type: String? = null) {

fun model(model: String) = apply { this.model = model }
fun color(color: String) = apply { this.color = color }
fun type(type: String) = apply { this.type = type }
fun build() = Car(model, color, type)
}
}

fun main() {
val car = Car.Builder()
.model("Ford Focus")
.color("Black")
.type("Type")
.build()
}
37 changes: 37 additions & 0 deletions src/main/kotlin/ru/otus/homework/Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.otus.homework

interface OrderCommand {
fun execute()
}

class OrderAddCommand(private val id: Long) : OrderCommand {
override fun execute() = println("Adding order with id: $id")
}

class OrderPayCommand(private val id: Long) : OrderCommand {
override fun execute() = println("Paying for order with id: $id")
}

class CommandProcessor {

private val queue = ArrayList<OrderCommand>()
fun addToQueue(orderCommand: OrderCommand): CommandProcessor =
apply {
queue.add(orderCommand)
}

fun processCommands(): CommandProcessor =
apply {
queue.forEach { it.execute() }
queue.clear()
}
}

fun main() {
CommandProcessor()
.addToQueue(OrderAddCommand(1L))
.addToQueue(OrderAddCommand(2L))
.addToQueue(OrderPayCommand(2L))
.addToQueue(OrderPayCommand(1L))
.processCommands()
}
58 changes: 58 additions & 0 deletions src/main/kotlin/ru/otus/homework/Decorator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.otus.homework

interface ChristmasTree {
fun decorate(): String
}

class PineChristmasTree : ChristmasTree {

override fun decorate() = "Christmas tree"
}

abstract class TreeDecorator
(private val tree: ChristmasTree) : ChristmasTree {

override fun decorate(): String {
return tree.decorate()
}
}

class BubbleLights(tree: ChristmasTree) : TreeDecorator(tree) {
override fun decorate(): String {
return super.decorate() + decorateWithBubbleLights()
}

private fun decorateWithBubbleLights(): String {
return " with Bubble Lights"
}
}

fun christmasTreeWithBubbleLights() {

val christmasTree = BubbleLights(PineChristmasTree())
val decoratedChristmasTree = christmasTree.decorate()
println(decoratedChristmasTree)
}

fun christmasTreeWithGarlands() {

val christmasTree = Garlands(PineChristmasTree())
val decoratedChristmasTree = christmasTree.decorate()
println(decoratedChristmasTree)
}

class Garlands(private val tree: ChristmasTree) : ChristmasTree by tree {

override fun decorate(): String {
return tree.decorate() + decorateWithGarlands()
}

private fun decorateWithGarlands(): String {
return " with Garlands"
}
}

fun main() {
christmasTreeWithBubbleLights()
christmasTreeWithGarlands()
}
14 changes: 14 additions & 0 deletions src/main/kotlin/ru/otus/homework/Singleton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.otus.homework

class Singleton private constructor() {

companion object {
val instance:Singleton by lazy {
Singleton()
}
}
}

fun main() {
val instance = Singleton.instance
}