KOTLIN_BROADCAST Telegram 1617
Чтоб скрыть детали реализации внутри библиотеки, иногда авторы используют классы с внутренней реализацией. Я такой подход использовал в ViewBindingPropertyDelegate для сокрытия реализации и отказа от if:

public object ViewBindingCache {

private var impl: ViewBindingCacheImpl = ViewBindingCacheImpl.Noop

internal fun <T : ViewBinding> getInflateWithLayoutInflater(
viewBindingClass: Class<T>
): InflateViewBinding<T> {
return impl.getInflateWithLayoutInflater(viewBindingClass)
}

internal fun <T : ViewBinding> getBind(
viewBindingClass: Class<T>
): BindViewBinding<T> {
return impl.getBind(viewBindingClass)
}

public fun clear() {
impl.clear()
}

public fun setEnabled(enabled: Boolean) {
impl = if (enabled) ViewBindingCacheImpl.Default() else ViewBindingCacheImpl.Noop
}
}

private sealed interface ViewBindingCacheImpl {
class Default : ViewBindingCacheImpl { ... }
data object Noop : ViewBindingCacheImpl
}


Если бы я не использовал внутреннюю реализацию, то подход был бы такой:
public object ViewBindingCache {

private var cacheEnabled = false
private val cache = mutableMapOf<...>()

internal fun <T : ViewBinding> getInflateWithLayoutInflater(
viewBindingClass: Class<T>
): InflateViewBinding<T> {
if (cacheEnabled) {
return ...
} else {
return ...
}
}

internal fun <T : ViewBinding> getBind(
viewBindingClass: Class<T>
): BindViewBinding<T> {
if (cacheEnabled) {
return ...
} else {
return ...
}
}

public fun clear() {
...
}

public fun setEnabled(enabled: Boolean) {
cache.clear()
this.cacheEnabled = enabled
}
}


Подобный подход используют в KMP, например Jetpack ViewModel. Также скрываются реализации различных lazy делегатов в Kotlin, но через фасад метода с параметром
fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>


#kotlin #AndroidBroadcast #БазаЗнаний



tgoop.com/kotlin_broadcast/1617
Create:
Last Update:

Чтоб скрыть детали реализации внутри библиотеки, иногда авторы используют классы с внутренней реализацией. Я такой подход использовал в ViewBindingPropertyDelegate для сокрытия реализации и отказа от if:

public object ViewBindingCache {

private var impl: ViewBindingCacheImpl = ViewBindingCacheImpl.Noop

internal fun <T : ViewBinding> getInflateWithLayoutInflater(
viewBindingClass: Class<T>
): InflateViewBinding<T> {
return impl.getInflateWithLayoutInflater(viewBindingClass)
}

internal fun <T : ViewBinding> getBind(
viewBindingClass: Class<T>
): BindViewBinding<T> {
return impl.getBind(viewBindingClass)
}

public fun clear() {
impl.clear()
}

public fun setEnabled(enabled: Boolean) {
impl = if (enabled) ViewBindingCacheImpl.Default() else ViewBindingCacheImpl.Noop
}
}

private sealed interface ViewBindingCacheImpl {
class Default : ViewBindingCacheImpl { ... }
data object Noop : ViewBindingCacheImpl
}


Если бы я не использовал внутреннюю реализацию, то подход был бы такой:
public object ViewBindingCache {

private var cacheEnabled = false
private val cache = mutableMapOf<...>()

internal fun <T : ViewBinding> getInflateWithLayoutInflater(
viewBindingClass: Class<T>
): InflateViewBinding<T> {
if (cacheEnabled) {
return ...
} else {
return ...
}
}

internal fun <T : ViewBinding> getBind(
viewBindingClass: Class<T>
): BindViewBinding<T> {
if (cacheEnabled) {
return ...
} else {
return ...
}
}

public fun clear() {
...
}

public fun setEnabled(enabled: Boolean) {
cache.clear()
this.cacheEnabled = enabled
}
}


Подобный подход используют в KMP, например Jetpack ViewModel. Также скрываются реализации различных lazy делегатов в Kotlin, но через фасад метода с параметром
fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>


#kotlin #AndroidBroadcast #БазаЗнаний

BY Kotlin Multiplatform Broadcast


Share with your friend now:
tgoop.com/kotlin_broadcast/1617

View MORE
Open in Telegram


Telegram News

Date: |

Just as the Bitcoin turmoil continues, crypto traders have taken to Telegram to voice their feelings. Crypto investors can reduce their anxiety about losses by joining the “Bear Market Screaming Therapy Group” on Telegram. "Doxxing content is forbidden on Telegram and our moderators routinely remove such content from around the world," said a spokesman for the messaging app, Remi Vaughn. How to create a business channel on Telegram? (Tutorial) With Bitcoin down 30% in the past week, some crypto traders have taken to Telegram to “voice” their feelings. Hui said the time period and nature of some offences “overlapped” and thus their prison terms could be served concurrently. The judge ordered Ng to be jailed for a total of six years and six months.
from us


Telegram Kotlin Multiplatform Broadcast
FROM American