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
33 changes: 33 additions & 0 deletions src/main/homework02/Builder.kt
Original file line number Diff line number Diff line change
@@ -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()
}
45 changes: 45 additions & 0 deletions src/main/homework02/Command.kt
Original file line number Diff line number Diff line change
@@ -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()
}
46 changes: 46 additions & 0 deletions src/main/homework02/Decorator.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
val peanutMilkShake = PeanutButterMilkShake(ConcreteMilkShake())
peanutMilkShake.getTaste()
val bananaMilkShake = BananaMilkShake(ConcreteMilkShake())
bananaMilkShake.getTaste()
}
9 changes: 9 additions & 0 deletions src/main/homework02/Singleton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main.homework02

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