Делегирование и состав по сути одинаковы?Android

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Делегирование и состав по сути одинаковы?

Сообщение Anonymous »


Under the hood , the below delegating code is creating instance of MVIDelegate within Viewmodel nd calling appropriate method of MVIDelegate as per the compiled bytecode.So this is composition only under the hood, and can be termed as Composition using Delegation?

Does the Delegation over Composition only provide better separation of concerns leading to more reusable, maintainable, and understandable code? Are there any other advantages of delegation over composition, particularly in terms of performance? Or are they essentially the same under the hood?
class ProfileViewModel : ViewModel(), MVI by MVIDelegate( initialViewState = UiState.Loading, ) { override fun onAction(uiAction: UiAction) { when (uiAction) { UiAction.OnIncreaseCountClick -> increaseCount() UiAction.OnDecreaseCountClick -> onDecreaseCountClick() } } private fun increaseCount() { updateUiState { copy(count = count + 1) } } private fun onDecreaseCountClick() { if (uiState.value.count > 0) { updateUiState { copy(count = count - 1) } } else { viewModelScope.emitSideEffect(SideEffect.ShowCountCanNotBeNegativeToast) } } }

Источник: https://stackoverflow.com/questions/781 ... r-the-hood
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»