diff --git a/src/main/homework02/Builder.kt b/src/main/homework02/Builder.kt new file mode 100644 index 0000000..f1c3bbb --- /dev/null +++ b/src/main/homework02/Builder.kt @@ -0,0 +1,33 @@ + +class FoodOrder private constructor( + val bread: String = "Flat bread", + val condiments: String?, + val meat: String?, + val fish: String?) { + + data class Builder( + var bread: String? = null, + var condiments: String? = null, + var meat: String? = null, + var fish: String? = null) { + + fun bread(bread: String) = apply { this.bread = bread } + fun condiments(condiments: String) = apply { this.condiments = condiments } + fun meat(meat: String) = apply { this.meat = meat } + fun fish(fish: String) = apply { this.fish = fish } + fun build() = FoodOrder(bread, condiments, meat, fish) + fun randomBuild() = bread(bread ?: "dry") + .condiments(condiments ?: "pepper") + .meat(meat ?: "beef") + .fish(fish?: "Tilapia") + .build() + } +} + +fun main() { + val foodOrder = FoodOrder.Builder() + .bread("white bread") + .meat("bacon") + .condiments("olive oil") + .build() +} diff --git a/src/main/homework02/Command.kt b/src/main/homework02/Command.kt new file mode 100644 index 0000000..797715d --- /dev/null +++ b/src/main/homework02/Command.kt @@ -0,0 +1,45 @@ +class Light { + fun turnOn() { + kotlin.io.println("The light is on") + } + + fun turnOff() { + kotlin.io.println("The light is off") + } +} + +interface Command { + fun execute() +} + +class TurnOnLightCommand(private val theLight: Light) : Command { + override fun execute() { + theLight.turnOn() + } +} + +class TurnOffLightCommand(private val theLight: Light) : Command { + override fun execute() { + theLight.turnOff() + } +} + +class Switch(private val flipUpCommand: Command, private val flipDownCommand: Command) { + fun flipUp() { + flipUpCommand.execute() + } + + fun flipDown() { + flipDownCommand.execute() + } +} + +fun main() { + val l = Light() + val switchUp: Command = TurnOnLightCommand(l) + val switchDown: Command = TurnOffLightCommand(l) + val s = Switch(switchUp, switchDown) + + s.flipUp() + s.flipDown() +} diff --git a/src/main/homework02/Decorator.kt b/src/main/homework02/Decorator.kt new file mode 100644 index 0000000..002ff04 --- /dev/null +++ b/src/main/homework02/Decorator.kt @@ -0,0 +1,46 @@ + +interface MilkShake { + fun getTaste() +} + +class ConcreteMilkShake : MilkShake { + override fun getTaste() { + println("It’s milk !") + } +} +open class MilkShakeDecorator(protected var milkShake: MilkShake) : MilkShake { + override fun getTaste() { + this.milkShake.getTaste() + } +} + +class BananaMilkShake(m:MilkShake) : MilkShakeDecorator(m){ + + override public fun getTaste(){ + super.getTaste (); + this.addTaste(); + println(" It’s Banana milk shake !"); + } + public fun addTaste(){ + println(" Adding Banana flavor to the milk shake !"); + } +} + +public class PeanutButterMilkShake(m:MilkShake) : MilkShakeDecorator(m){ + + override public fun getTaste(){ + super.getTaste (); + this.addTaste(); + println(" It’s Peanut butter milk shake !"); + } + public fun addTaste(){ + println(" Adding Peanut butter flavor to the milk shake !"); + } +} + +fun main(args: Array) { + val peanutMilkShake = PeanutButterMilkShake(ConcreteMilkShake()) + peanutMilkShake.getTaste() + val bananaMilkShake = BananaMilkShake(ConcreteMilkShake()) + bananaMilkShake.getTaste() +} diff --git a/src/main/homework02/Singleton.kt b/src/main/homework02/Singleton.kt new file mode 100644 index 0000000..9a5e660 --- /dev/null +++ b/src/main/homework02/Singleton.kt @@ -0,0 +1,9 @@ +package main.homework02 + +class Singleton private constructor() { + companion object { + val instance:Singleton by lazy { + Singleton() + } + } +} \ No newline at end of file