-
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?
Conversation
| 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 { |
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 , 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() |
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, тут все хорошо, но, КМК, можно улучшить и убрать дублирование логики у детей. Мы всегда прибавляем цифры и строки, поэтому
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"
}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.
Это красиво, я не додумался
| override fun description(): String { | ||
| TODO("Not yet implemented") | ||
| } | ||
| class MilkDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { |
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 , и тут, согласно верхнему:
class MilkDecorator(coffee: Coffee) : DecoratorCoffee(coffee) {
override val extraCost: Int = 50
override val extraDesc: String = "Milk"
}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.
Так выглядит намного чище, и в декораторе спрятана логика 🤝
|
|
||
| // если бы можно было переделать конструктор NonEmptyStringDelegate(), | ||
| // было бы проще с передачей значения | ||
| private val fullNameDelegate = NonEmptyStringDelegate().apply { |
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, можно.
Делегат:
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)
}
}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.
А что я постеснялся.. 😁
Спасибо!
No description provided.