In this tutorial, we will learn about android architecture pattern (MVC, MVP, MVVM) in detail.
Contents
What is Android Architecture Components?
Architecture patterns in android are used to separate concerns in order to structure the project’s code and give it a modular design (separated code parts). The following are the most popular Android architectures used by developers:
- MVC (Model — View — Controller).
- MVP (Model — View — Presenter)
- MVVM (Model — View — ViewModel)
Need of Architecture
- Scalability and Maintainability of an application.
- To achieve this – We implement architecture patterns- MVC , MVP, MVVM etc.
Architecture pattern mainly focuses on:
- Separation of concerns
- Unit testing
1: MVVM Pattern
Model-View-ViewModel is abbreviated as MVVM. This pattern allows for two-way data binding between the view and the View model. This allows for the automatic propagation of changes from the state of view model to the View. Typically, the observer pattern is used by the view model to notify changes in the view model.
Model:
The Model is a set of classes that describe the business logic and data. It also defines data business rules, which describe how data can be changed and manipulated.
View:
The View represents the UI components such as CSS, jQuery, HTML, and so on. It is only in charge of displaying the data received from the controller as a result. This also converts the model(s) to UI.
ViewModel:
The View Model is responsible for exposing methods, commands, and other properties that help in the maintenance of the view’s state, the manipulation of the model as a result of actions on the view, and the triggering of events in the view itself.
A simple Notes Application using MVVM architecture pattern Room Database Android Example- Building a Notes App
Features of the MVVM pattern
- The user interacts with the View.
- Because View and ViewModel have a many-to-one relationship, multiple Views can be mapped to a single ViewModel.
- A view has a reference to ViewModel, but View Model contains no information about the View.
- Allows for two-way data binding between the View and ViewModel.
2: MVC Pattern
MVC stands for Model-View-Controller. It is a software design pattern that was first used in the 1970s. Furthermore, the MVC pattern enforces a separation of concerns, which means that the domain model and controller logic are decoupled from the user interface (view). As a result, application maintenance and testing become simpler and easier.
The MVC design pattern divides an application into three major components: View, Model, and Controller
Model:
The Model is a collection of classes that describe the business logic (business model) and data access operations (data model). It also defines data business rules, which describe how data can be changed and manipulated.
View:
The View represents the UI components such as CSS, jQuery, HTML, and so on. It is only in charge of displaying the data received from the controller as a result. This also converts the model(s) to UI.
Controller:
The Controller is in charge of handling incoming requests. It receives user input via the View, then processes the user’s data with the help of the Model before returning the results to the View. It usually serves as the coordinator between the view and model.
3: MVP Pattern
This pattern is similar to the MVC pattern in which the controller has been replaced by the presenter. This design pattern divides an application into three major components: the Model, the View, and the Presenter.
Model
The Model is a collection of classes that describe the business logic and data. It also defines data business rules, which describe how data can be changed and manipulated.
View
The View represents the UI components such as CSS, jQuery, HTML, and so on. It is only in charge of displaying the data received from the presenter as a result. This also converts the model(s) to UI.
Presenter
The Presenter is responsible for handling all UI events on the view’s behalf. This receives user input via the View, then processes the user’s data with the help of the Model before returning the results to the View. View and presenter, unlike view and controller, are completely decoupled from each other and communicate via an interface. Also, as a controller, the presenter does not manage the incoming request traffic.
Features of MVP Pattern
- The user interacts with the View.
- A one-to-one relationship exists between View and Presenter, which means that one View is mapped to only one Presenter.
- View refers to Presenter but does not refer to Model.
- Allows for two-way communication between the viewer and the presenter.
Difference between MVVM and MVC Pattern
MVC | MVVM |
---|---|
The controller is the entry point to the Application. | The view is the entry point to the Application. |
One too many relationships between Controller & View. | One too many relationships between View & View models. |
View Does not have reference to the Controller | View has references to the View-Model. |
MVC is Old Model | MVVM is a relatively New Model. |
Difficult to read, change, unit test, and reuse this Model | The debugging process will be complicated when we have complex data bindings. |
MVC Model component can be tested separately from the user | Easy for separate unit testing and code is event-driven. |
Summary
In MVC, the controller is the application’s entry point, whereas in MVVM, the view is the application’s entry point. MVC Model components can be tested independently of the user, whereas MVVM allows for separate unit testing and event-driven code.
- We hope that this guide will assist you in understanding all the concepts of architecture pattern android. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts of architecture pattern android. Still, if you have any problems regarding this, please post them in the comments section, we will be glad to assist you.
android architecture pattern, android architecture pattern, android architecture pattern, android architecture pattern, android architecture pattern,android architecture pattern, android architecture pattern, android architecture pattern, android architecture pattern, android architecture pattern android architecture pattern, android architecture pattern, android architecture pattern,
Pingback: Introduction to Kotlin Coroutines in Android - Developers Dome