Developers come across various situations where they can easily get the job done by using reusable coding solutions. Architecture Patterns are such reusable solutions which a developer can use to save his time and efforts.
In the earlier days of Android app development
, Architecture patterns weren’t a popular development element. At that point in time, the primitive pattern was the closest thing to having architecture patterns.
Here’s how primitive pattern worked:
The two layers of the primitive pattern worked individually to control specific tasks. While data handling was done at the data layer, the view layer was responsible for handling the visualization of the data, i.e., UX/UI.
Though as simple as the architecture may look, the primitive pattern had several drawbacks which made the developers drop it and look for other effective solutions. Some of these drawbacks include:
Complications While Updating the database.
Lack of communication between the data layer and the View layer.
Time-Consuming Debugging with too many complexities.
Impossibility to test the business logic, resulting in unexpected errors.
Since these drawbacks were too complicated to tackle, developers came up with other architecture patterns which managed to simplify the entire development process. MVC (Model View Controller) was first of these patterns.
The three elements of the MVC model did simplify the coding solutions to a certain extent but it wasn’t an optimal solution for the current marketing standards.
It is when advanced architecture patterns like MVP and MVVM came into the picture. Fortunately, these patterns are less complicated and make the entire Android app development process quite easy. So, in this blog, we will be sharing an in-depth comparison of MVP vs MVVM architecture pattern.
MVP (Model View Presenter)
The MVP model is basically an improvement over the conventional MVC model. In this case, the Controller is replaced by the Presenter. The modification was much needed to enhance the functionality of MVC for better coding execution and better UI design.
Here’s how the MVP pattern works.
Model
The Model is responsible for handling the local database interactions. It responds to the queries by fetching or updating the information at the database. It means that if the function being called is requesting certain information from the database, the Model will locate it and respond accordingly. The Model only interacts with the Presenter and doesn’t have any idea about the existence of View at all.
View
Like Model, the View also interacts with the presenter only to visually display the data to the user. It also notifies the Presenter regarding user inputs. View, however, doesn’t process the data at all. In fact, the View doesn’t directly interact with the Model as well. Its only job is to display the information to the user.
Presenter
The presenter is the layer which acts as a mediator between the other two layers. It interacts with both the Model and the View to transfer the data between two layers. It’s only responsibility is to collect user input and pass it to the Model. The model then processes the data accordingly and passes the result to Presenter again, which is then further passed to View.
Pros and Cons of MVP
While the MVP model has several improvements over the conventional MVC pattern, it still has pros and cons of its own. Let’s take a look at some of the advantages and disadvantages of MVP architecture pattern.
Pros
The View layer is extremely light as it only visualizes the data and doesn’t perform any data processing operations.
Easy to test the code as each interface is separately divided.
Cons
Since each interface handles the conversation to extreme levels, it is crucial to code each interface precisely.
It takes more time to code the entire interface.
Model View ViewModel (MVVM)
MVVM is a further improvement over the MVP architecture pattern. In this pattern, the Presenter is replaced by the ViewModel which handles the entire data processing. However, unlike the MVP pattern, the ViewModel, in this case, doesn’t have any idea about the existence of the View or the Model at all.
It operates individually, which results in a smooth flow of data between the three layers. One of the major differences that MVVM has from MVP is the flow of data. In MVVM, the data flows in separated streams and can be collected by the consumer accordingly.
Here’s how the MVVM model operates:
Model:
Like the previous architecture pattern, the Model handles the entire data. Also known as the DataModel, it’s only responsibility is to expose data for the ViewModel after retrieving it from different sources including the database as well as the network layer. Basically, the DataModel layer holds the entire business logic.
View:
View, in this case, performs the same task like MVP model, i.e., visualization of the data. It is basically the user-interface which is responsible for displaying and collecting the data through user events.
It then passes the data to the ViewModel in the form of data streams. Once the ViewModel has the desired response to the user query, it again passes this information back to the view.
ViewModel:
The ViewModel performs all the logical operations on the data retrieved from the DataModel. It accepts the data streams from the DataModel and binds them together to perform further operations. Once it has processed the data, it passes the data to the View layer in the form of data streams too.
In the MVVM architecture pattern, the data consumer, i.e., DataModel is familiar with the existence of the data producer, i.e., ViewModel. The entire ViewModel works individually and doesn’t have any idea about the existence of the DataModel as well as the View.
Pros and Cons of MVVM
Even the MVVM model has pros and cons of their own. Here are some of the advantages and disadvantages of the MVVM model.
Pros
Binding operators can be used to write all the XML-attributes.
Review at the compilation stage.
Generate individual components to improve the testing process.
Cons
The developers can only operate the View using two methods, i.e., DataBinding and View methods.
Comparison Between MVP and MVVM
Now, that you know the difference between MVP and MVVM, let’s compare some of the features of both MVP and MVVM architecture pattern.
JAVA Classes
In both the architecture patterns, there is a huge number of JAVA classes. However, the code length of the classes in MVVM is comparatively low.
Maintainability
The learning curve for MVP is easier than MVVM and as a result, adding new features in MVP is extremely easy too. However, when it comes to MVVM, you need technical expertise to add new features.
Data Input
In the case of MVP, the data input happens at the View. Similarly, the MVVM receives data input from View too.
Conclusion
Architecture patterns have a significant role in building a mobile app from scratch. However, you can’t solely rely on one architecture pattern for the entire app development process.
Sometimes, you may even have to switch to a different pattern and that’s completely fine. The thing that you should remember, though, is to pick the right architecture pattern at the right time.