Skip to content

Conversation

@dbulygin
Copy link

No description provided.

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 {
Copy link
Collaborator

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()
Copy link
Collaborator

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"
}

Copy link
Author

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) {
Copy link
Collaborator

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"
}

Copy link
Author

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 {
Copy link
Collaborator

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)
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что я постеснялся.. 😁
Спасибо!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants