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
39 changes: 39 additions & 0 deletions Builder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Person {
private var name: String? = null
private var surname: String? = null
private var age = 0

class Builder {
private val newPerson: Person
fun withName(name: String?): Builder {
newPerson.name = name
return this
}

fun withSurname(surname: String?): Builder {
newPerson.surname = surname
return this
}

fun withAge(age: Int): Builder {
newPerson.age = age
return this
}

fun build(): Person {
return newPerson
}

init {
newPerson = Person()
}
}
}

fun main(){
val myPerson = Person.Builder()
.withName("John")
.withSurname("Connor")
.withAge(39)
.build()
}
33 changes: 33 additions & 0 deletions Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
interface Command{
fun execute()
}
class Lamp{
fun on(){
println("Lamp is on")
}
fun off(){
println("Lamp is off")
}
}
class OnLamp(val lamp: Lamp): Command {
override fun execute() {
lamp.on()
}
}
class OffLamp(val lamp: Lamp): Command {
override fun execute() {
lamp.off()
}
}
class LampInvoker(var command: Command) {
fun execute() {
command.execute()
}
}
fun main(){
val lamp = Lamp()
val onLamp = OnLamp(lamp)
val offLamp = OffLamp(lamp)
val lampInvoker = LampInvoker(onLamp)
lampInvoker.execute()
}
25 changes: 25 additions & 0 deletions Decorator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
interface Machine{
fun drive(){
}
}

open class Car: Machine{
override fun drive() {
println("This is car")
}
}

class ColoredCar(val sample:Car, val color: String): Car(){
override fun drive() {
sample.drive()
println("Color of car is $color")
}
}

fun main() {
val car = Car()
car.drive()
val color = readLine()!!.toString()
val coloredCar = ColoredCar(car,color)
coloredCar.drive()
}
24 changes: 24 additions & 0 deletions SingleLazy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
object Singleton{

init {
println("1")
}

object Holder {
val INSTANCE = Singleton
}

object Lazy {
val instance: Singleton by lazy {
Holder.INSTANCE
}
}

var b: String = "2"
}
fun main() {
println(Singleton.Lazy.instance.b)
Singleton.Lazy.instance.b = "3"
println(Singleton.Lazy.instance.b)
println(Singleton.Lazy.instance.b)
}