Sobes.tech
Middle

Do you know how MVVM differs from MVP?

sobes.tech AI

Answer from AI

MVVM (Model-View-ViewModel) and MVP (Model-View-Presenter) are architecture patterns that separate application components for better maintainability and testability.

Main differences:

  • Connection between View and Model:
    • MVP: View and Model are not directly connected. The Presenter acts as an intermediary, receiving data from the Model and passing it to the View.
    • MVVM: The View is directly connected to the ViewModel through data binding mechanisms. The ViewModel provides data that the View displays.
  • Presentation Logic:
    • MVP: The presentation logic resides in the Presenter. The Presenter handles user input, interacts with the Model, and updates the View.
    • MVVM: The presentation logic resides in the ViewModel. The ViewModel contains UI state and business logic necessary for displaying and processing data.
  • Testability:
    • MVP: The Presenter is easy to test in isolation because it does not depend on a specific View. The View is tested separately, often using mocks of the Presenter.
    • MVVM: The ViewModel is easy to test in isolation because it does not depend on the View. The View is tested separately, usually with verification of correct binding to the ViewModel.
  • Dependencies:
    • MVP: The View depends on the Presenter (via interface), and the Presenter depends on the View (via interface) and the Model.
    • MVVM: The View depends on the ViewModel. The ViewModel depends on the Model.
// Example structure for MVP
interface MainActivityContract {
    interface View {
        fun showData(data: String)
        fun showError(message: String)
    }

    interface Presenter {
        fun loadData()
        fun onDestroy()
    }
}

class MainActivity : AppCompatActivity(), MainActivityContract.View {
    private lateinit var presenter: MainActivityContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        presenter = MainActivityPresenter(this, Model())
        presenter.loadData()
    }

    override fun showData(data: String) {
        // Update UI
    }

    override fun showError(message: String) {
        // Show error
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroy()
    }
}

class MainActivityPresenter(
    private val view: MainActivityContract.View,
    private val model: Model
) : MainActivityContract.Presenter {

    override fun loadData() {
        // Get data from Model
        val data = model.getData()
        if (data != null) {
            view.showData(data)
        } else {
            view.showError("Error loading data")
        }
    }

    override fun onDestroy() {
        // Clean up resources
    }
}

class Model {
    fun getData(): String? {
        // Data retrieval logic
        return "Sample data from Model"
    }
}
// Example structure for MVVM
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        binding.viewModel = viewModel
        binding.lifecycleOwner = this // Important for LiveData
    }
}

class MainViewModel : ViewModel() {
    private val _data = MutableLiveData<String>()
    val data: LiveData<String> = _data

    init {
        loadData()
    }

    private fun loadData() {
        // Data retrieval logic from Model or repository
        _data.value = "Sample data from ViewModel"
    }
}

// In the activity_main.xml file, data binding is used:
/*
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewModel"
            type="com.example.yourapp.MainViewModel" />
    </data>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{viewModel.data}" />
</layout>
*/

MVVM is often used with data binding libraries (like Android Data Binding) and architecture components (ViewModel, LiveData, Room) to simplify development and enhance UI reactivity.