-
Notifications
You must be signed in to change notification settings - Fork 50
Решение к домашней работе #45 #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,32 +20,22 @@ class SimpleCoffee : Coffee { | |
| override fun description() = "Простой кофе" | ||
| } | ||
|
|
||
| class MilkDecorator(private val coffee: Coffee) : Coffee { | ||
| override fun cost(): Int { | ||
| TODO("Not yet implemented") | ||
| } | ||
|
|
||
| override fun description(): String { | ||
| TODO("Not yet implemented") | ||
| } | ||
| abstract class DecoratorCoffee(private val coffee: Coffee): Coffee{ | ||
| override fun cost() = coffee.cost() | ||
| override fun description() = coffee.description() | ||
| } | ||
|
|
||
| class SugarDecorator(private val coffee: Coffee) : Coffee { | ||
| override fun cost(): Int { | ||
| TODO("Not yet implemented") | ||
| } | ||
|
|
||
| override fun description(): String { | ||
| TODO("Not yet implemented") | ||
| } | ||
| class MilkDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbulygin , и тут, согласно верхнему: class MilkDecorator(coffee: Coffee) : DecoratorCoffee(coffee) {
override val extraCost: Int = 50
override val extraDesc: String = "Milk"
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так выглядит намного чище, и в декораторе спрятана логика 🤝 |
||
| override fun cost() = super.cost() + 50 | ||
| override fun description() = super.description() + ", молоко" | ||
| } | ||
|
|
||
| class VanillaDecorator(private val coffee: Coffee) : Coffee { | ||
| override fun cost(): Int { | ||
| TODO("Not yet implemented") | ||
| } | ||
| class SugarDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { | ||
| override fun cost() = super.cost() + 20 | ||
| override fun description() = super.description() + ", сахар" | ||
| } | ||
|
|
||
| override fun description(): String { | ||
| TODO("Not yet implemented") | ||
| } | ||
| class VanillaDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { | ||
| override fun cost() = super.cost() + 70 | ||
| override fun description() = super.description() + ", ваниль" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| @file:Suppress("RemoveRedundantQualifierName") | ||
|
|
||
| package ru.otus.homework.homework | ||
| import kotlin.properties.Delegates | ||
|
|
||
| /** | ||
| * Профиль пользователя | ||
|
|
@@ -27,6 +28,7 @@ interface UserProfile { | |
| /** | ||
| * Создает профиль пользователя | ||
| */ | ||
| //override var email: String by Delegates.vetoable() | ||
| fun create(fullName: String, email: String): UserProfile { | ||
| require(fullName.isNotBlank()) { "Full name should not be empty" } | ||
| require(email.isNotBlank() && emailRegex.matches(email)) { "Invalid email" } | ||
|
|
@@ -37,7 +39,7 @@ interface UserProfile { | |
| * Creates user profile with logging | ||
| */ | ||
| fun createWithLogging(fullName: String, email: String): UserProfile.Logging { | ||
| TODO("Implement `createWithLogging` function") | ||
| return LoggingProfile(fullName, email) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -50,4 +52,56 @@ private val emailRegex = Regex("^[A-Za-z](.*)([@])(.+)(\\.)(.+)") | |
| /** | ||
| * Реализация простого [UserProfile]. | ||
| */ | ||
| private class ProfileImplementation(override var fullName: String, override var email: String): UserProfile | ||
| private class ProfileImplementation(initFullNAme: String, initEmail: String): UserProfile { | ||
| override var email: String by Delegates.vetoable(initEmail) { _, old, new -> | ||
| when { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbulygin , override var email: String by Delegates.vetoable(initEmail) { _, old, new ->
emailRegex.matches(new)
} |
||
| !emailRegex.matches(new) -> { | ||
| false | ||
| } | ||
|
|
||
| else -> true | ||
| } | ||
| } | ||
|
|
||
| // если бы можно было переделать конструктор NonEmptyStringDelegate(), | ||
| // было бы проще с передачей значения | ||
| private val fullNameDelegate = NonEmptyStringDelegate().apply { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbulygin, можно. class NonEmptyStringDelegate(private var value: String = "") {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return value
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) {
value = newValue.ifBlank { value }
}
}Профиль: private class ProfileImplementation(fullName: String, email: String): UserProfile {
override var fullName: String by NonEmptyStringDelegate(fullName)
override var email: String by Delegates.vetoable(email) { _, _, new ->
new.isNotBlank() && emailRegex.matches(new)
}
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А что я постеснялся.. 😁 |
||
| init(initFullNAme) | ||
| } | ||
| override var fullName: String by fullNameDelegate | ||
| } | ||
|
|
||
| private class LoggingProfile(initFullName: String, initEmail: String) : UserProfile.Logging { | ||
| private val logs = mutableListOf<String>() | ||
|
|
||
| override fun getLog(): List<String> = logs.toList() | ||
|
|
||
| private val fullNameDelegate = NonEmptyStringDelegate().apply { | ||
| init(initFullName) | ||
| } | ||
| override var fullName: String | ||
| get() = fullNameDelegate.getValue(this, ::fullName) | ||
| set(value) { | ||
| val oldValue = fullName | ||
| fullNameDelegate.setValue(this, ::fullName, value) | ||
| if (fullName != oldValue) { | ||
| addLog("Changing `fullName` from '$oldValue' to '$fullName'") | ||
| } else { | ||
| addLog("Bad FullName: $fullName") | ||
| } | ||
| } | ||
|
|
||
| override var email: String by Delegates.vetoable(initEmail) { _, old, new -> | ||
| val isValid = emailRegex.matches(new) | ||
| if (isValid) { | ||
| addLog("Changing `email` from '$old' to '$new'") | ||
| } else { | ||
| addLog("Bad email: $new") | ||
| } | ||
| isValid | ||
| } | ||
|
|
||
| private fun addLog(message: String) { | ||
| logs.add(message) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dbulygin, тут все хорошо, но, КМК, можно улучшить и убрать дублирование логики у детей. Мы всегда прибавляем цифры и строки, поэтому
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это красиво, я не додумался