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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Project exclude paths
/.gradle/
/.gradle/
/build/
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

3 changes: 3 additions & 0 deletions .idea/misc.xml

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

5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm' version '1.7.21'
}

test {
useJUnitPlatform()
}

group 'ru.otus'
version '1.0-SNAPSHOT'

Expand All @@ -11,4 +15,5 @@ repositories {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation 'org.junit.jupiter:junit-jupiter:5.9.2'
}
36 changes: 36 additions & 0 deletions src/main/kotlin/ru/otus/homework2/Builder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.otus.homework2


class Account(private val config: Builder) {

fun info(): String {
return "Имя: ${config.name}. Никнейм: ${config.nickname}"
}

fun mail(): String {
return "Email: ${config.email}"
}

companion object Builder {
private var name: String? = null
private var nickname: String? = null
private var email: String? = null

fun name(value: String) = apply { name = value }
fun nickname(value: String) = apply { nickname = value }
fun email() = apply { email = "$nickname@mail.ru" }

fun build(): Account {
return Account(this)
}
}
}
fun main() {
val account = Account
.name("Evgeniy")
.nickname("zackzevs")
.email()
.build()
println(account.info())
println(account.mail())
}
37 changes: 37 additions & 0 deletions src/main/kotlin/ru/otus/homework2/Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.otus.homework2

interface Command {
fun execute()
}
class Text {
fun print() {
println("Привет")
}
fun print2() {
println("Пока")
}
}
class ObjText(private val string: Text) : Command {
override fun execute() {
string.print()
}
}
class ObjText2(private val string: Text) : Command {
override fun execute() {
string.print2()
}
}
class Invoker (var command: Command) {
fun doExecute() {
command.execute()
}
}
fun main() {
val text = Text()
val objText = ObjText(text)
val objText2 = ObjText2(text)
val invoker = Invoker(objText)
invoker.doExecute()
invoker.command = objText2
invoker.doExecute()
}
41 changes: 41 additions & 0 deletions src/main/kotlin/ru/otus/homework2/Decorator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.otus.homework2

interface Coffee {
fun decorate(): String
}

class BlackCoffee : Coffee {
override fun decorate() = "Кофе"
}

abstract class CoffeeDecorator(private val coffee: Coffee) : Coffee {
override fun decorate(): String {
return coffee.decorate()
}
}
class WithMilk(coffee: Coffee) : CoffeeDecorator(coffee) {
override fun decorate(): String {
return super.decorate() + decorateWithMilk()
}
private fun decorateWithMilk(): String {
return ", молоко"
}
}
class WithSugar(coffee: Coffee) : CoffeeDecorator(coffee) {
override fun decorate(): String {
return super.decorate() + decorateWithSugar()
}
private fun decorateWithSugar(): String {
return ", сахар"
}
}
fun main() {
val blackCoffee = BlackCoffee()
val withMilkCoffee = WithMilk(blackCoffee)
val withSugarCoffee = WithSugar(blackCoffee)
val withSugarAndWithMilkCoffee = WithMilk(withSugarCoffee)
println(blackCoffee.decorate())
println(withMilkCoffee.decorate())
println(withSugarCoffee.decorate())
println(withSugarAndWithMilkCoffee.decorate())
}
27 changes: 27 additions & 0 deletions src/main/kotlin/ru/otus/homework2/Singlton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.otus.homework2

class Singleton private constructor(){

init {
println("Singleton class вызван.")
}
fun start(){
println("Начали")
}
fun end(){
println("Закончили")
}
companion object {
val instance: Singleton by lazy { Singleton() }
}
}

fun main() {
val one = Singleton.instance
one.start()
one.end()
val two = Singleton.instance
two.start()
two.end()
println(one === two)
}