Решение к домашней работе #45#35
Open
dbulygin wants to merge 1 commit intoAndroid-Developer-Basic:masterfrom
Open
Решение к домашней работе #45#35dbulygin wants to merge 1 commit intoAndroid-Developer-Basic:masterfrom
dbulygin wants to merge 1 commit intoAndroid-Developer-Basic:masterfrom
Conversation
motorro
reviewed
Aug 28, 2025
| private class ProfileImplementation(override var fullName: String, override var email: String): UserProfile No newline at end of file | ||
| 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.
@dbulygin , matches уже возвращает Boolean. Поэтому, для лучшей читаемости можно написать:
override var email: String by Delegates.vetoable(initEmail) { _, old, new ->
emailRegex.matches(new)
}| TODO("Not yet implemented") | ||
| } | ||
| abstract class DecoratorCoffee(private val coffee: Coffee): Coffee{ | ||
| override fun cost() = coffee.cost() |
Collaborator
There was a problem hiding this comment.
@dbulygin, тут все хорошо, но, КМК, можно улучшить и убрать дублирование логики у детей. Мы всегда прибавляем цифры и строки, поэтому
abstract class DecoratorCoffee(private val coffee: Coffee): Coffee{
abstract val extraCost: Int
abstract val extraDesc: String
override fun cost() = coffee.cost() + extraCost
override fun description() = "${coffee.description()}, $extraDesc"
}| override fun description(): String { | ||
| TODO("Not yet implemented") | ||
| } | ||
| class MilkDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { |
Collaborator
There was a problem hiding this comment.
@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.
Так выглядит намного чище, и в декораторе спрятана логика 🤝
|
|
||
| // если бы можно было переделать конструктор NonEmptyStringDelegate(), | ||
| // было бы проще с передачей значения | ||
| private val fullNameDelegate = NonEmptyStringDelegate().apply { |
Collaborator
There was a problem hiding this comment.
@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.
А что я постеснялся.. 😁
Спасибо!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.