Developers always prefer to develop an Android application by applying a software architecture pattern. An architecture pattern gives modularity to project files and ensures that all code is covered in unit tests. It makes it easy for developers to maintain the software and extend the application features in the future. There are some architectures that are very popular among developers and one of them is the Model-View-Controller (MVC) pattern. The MVC pattern suggests dividing the code into 3 components. When creating the application class/file, the developer should categorize it into one of the following three layers:
- Model: This component stores the application data. It has no knowledge about the interface. The model is responsible for handling the domain logic (real world business rules) and communication with the database and network layers.
- View: It is the layer UI (user interface) containing components that are visible on the screen. In addition, it provides the visualization of the data stored in the Model and offers interaction with the user.
- Controller: This component establishes the relationship between the View and the Model. Contains the main logic of the application and reports user behavior and updates the Model as needed.
Despite applying the MVC schema to give a modular design to the application, the layers of code depend on each other. In this pattern, View and Controller both depend on Model. Multiple approaches are possible to apply the MVC pattern in the project:
- Approach 1: Activities and fragments can play the role of controller and are responsible for updating the View.
In the MVC architecture, the controller updates the application data and the View fetches the data. Since the Model component is separate, it could be tested independently of the UI. Also, if the View layer respects the single responsibility principle, then its role is only to update the Controller for each user event and only display data from the Model, without implementing any business. logic. In this case, the UI tests should be enough to cover the functionality of the view.
MVC Architecture Example
To understand more clearly the implementation of the MVC architecture pattern, click here there is a simple example of an android application. This app will have 3 buttons and each one of them shows the count of how many times the user has clicked on that particular button. To develop this application, the code has been separated as follows:
- Controller and View will be managed by the Activity. Each time the user clicks the buttons, the activity instructs the Model to handle subsequent operations. The activity will act as an observer.
- The Model will be a separate class that will contain the data to be displayed. Operations on the data will be performed by functions of this class and after updating the data values, this observable class notifies the observer (activity) about the change.
The following is the complete step-by-step implementation of this Android application using the MVC architecture pattern:
Note: The following steps are performed in Android Studio version 4.0
Step 1: Create a new project
- Click File, then in New =The data provided by the Model will be used by the View and the appropriate changes will be made to the Activity.
Output
Advantages of the MVC architecture pattern
- The MVC pattern increases the testability of the code and makes it easier to implement new features, since it supports very well the separation of concerns.
- Model and Controller unit tests are possible, since there is no do not extend or use any Android classes.
- The View’s functionality can be verified through UI testing if the View respects the single responsibility principle (update controller and display model data without implement domain logic)
Disadvantages of the MVC architecture pattern
- Layers of code depend on each other even if MVC is applied correctly.
- There are no parameters to handle the UI logic, i.e. how to display the data.
.