Делегирование и состав по сути одинаковы? ⇐ Android
Делегирование и состав по сути одинаковы?
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
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение