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
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules/homework2.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/main/kotlin/Builder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Product(private val name: String, private val price: Double, private val weight: Double, private val color: String) {
override fun toString(): String {
return "Product(name='$name', price=$price, weight=$weight, color='$color')"
}
}

class ProductBuilder {
private var name: String = ""
private var price: Double = 0.0
private var weight: Double = 0.0
private var color: String = ""

fun setName(name: String): ProductBuilder {
this.name = name
return this
}

fun setPrice(price: Double): ProductBuilder {
this.price = price
return this
}

fun setWeight(weight: Double): ProductBuilder {
this.weight = weight
return this
}

fun setColor(color: String): ProductBuilder {
this.color = color
return this
}

fun build(): Product {
return Product(name, price, weight, color)
}
}

fun main() {

val product = ProductBuilder()
.setName("Example Product")
.setPrice(50.5)
.setWeight(2.2)
.setColor("Red")
.build()

println(product)
}
56 changes: 56 additions & 0 deletions src/main/kotlin/Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
interface Command {
fun execute()
}

class LightOnCommand(private val light: Light) : Command {
override fun execute() {
light.on()
}
}

class LightOffCommand(private val light: Light) : Command {
override fun execute() {
light.off()
}
}


class Light {
fun on() {
println("Light on")
}

fun off() {
println("Light off")
}
}


class RemoteControl {
private val commands = mutableListOf<Command>()

fun setCommand(command: Command) {
commands.add(command)
}

fun executeCommands() {
for (command in commands) {
command.execute()
}
commands.clear()
}
}


fun main() {
val remoteControl = RemoteControl()

val light = Light()
val lightOnCommand = LightOnCommand(light)
val lightOffCommand = LightOffCommand(light)

remoteControl.setCommand(lightOnCommand)
remoteControl.setCommand(lightOffCommand)

remoteControl.executeCommands()
}
58 changes: 58 additions & 0 deletions src/main/kotlin/Decorator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

interface Coffee {
fun cost(): Double
fun ingredients(): String
}


class SimpleCoffee : Coffee {
override fun cost(): Double {
return 1.0
}

override fun ingredients(): String {
return "Simple coffee"
}
}

abstract class CoffeeDecorator(private val decoratedCoffee: Coffee) : Coffee {
override fun cost(): Double {
return decoratedCoffee.cost()
}

override fun ingredients(): String {
return decoratedCoffee.ingredients()
}
}


class Milk(private val decoratedCoffee: Coffee) : CoffeeDecorator(decoratedCoffee) {
override fun cost(): Double {
return super.cost() + 0.5
}

override fun ingredients(): String {
return super.ingredients() + ", Milk"
}
}


class Whip(private val decoratedCoffee: Coffee) : CoffeeDecorator(decoratedCoffee) {
override fun cost(): Double {
return super.cost() + 0.7
}

override fun ingredients(): String {
return super.ingredients() + ", Whip"
}
}

fun main() {

var someCoffee: Coffee = SimpleCoffee()

someCoffee = Milk(someCoffee)
someCoffee = Whip(someCoffee)

println("Cost: ${someCoffee.cost()}; Ingredients: ${someCoffee.ingredients()}")
}
11 changes: 11 additions & 0 deletions src/main/kotlin/Singleton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Singleton private constructor() {
companion object {
val instance: Singleton by lazy { Singleton() }
}
}
fun main() {
val singleton: Singleton = Singleton.instance
val singleton2: Singleton = Singleton.instance

println(singleton === singleton2)
}