Software Development Exam  >  Software Development Notes  >  Android Architecture Patterns

Android Architecture Patterns

Android Architecture Patterns

Last Updated : 24 Jan, 2021

When developers engage in building a dynamic mobile application that will evolve its features based on user requirements, writing core logic within activities or fragments becomes impractical. To enhance code structure and impart a modular design (separated code segments), architecture patterns are implemented to segregate concerns. The prevalent android architectures favored by developers encompass:

  • MVC (Model - View - Controller)
  • MVP (Model - View - Presenter)
  • MVVM (Model - View - ViewModel)

MVC (Model - View - Controller)

The fundamental concept behind these patterns is to systematically organize the project to facilitate comprehensive Unit Testing coverage. Additionally, it greatly aids in software maintenance, enabling seamless addition or removal of features while allowing developers to monitor critical logic segments.

The Model-View-Controller(MVC) Pattern

The MVC pattern stands as the oldest android app architecture, advocating the division of code into three distinct layers:

  • Model: This layer serves as the data repository, responsible for managing domain logic (real-world business rules) and interfacing with the database and network layers.
  • View: The UI(User Interface) layer responsible for data visualization sourced from the Model.
  • Controller: This layer encapsulates core logic, reacting to user behavior and updating the Model as necessary.

MVC Pattern Overview

  • Model: This layer functions as a data store, managing domain logic and interactions with databases and networks.
  • View: The UI layer responsible for presenting data from the Model to users.
  • Controller: Core logic layer that reacts to user actions, updating the Model accordingly.
MVC Pattern Overview

In the MVC architecture, both the View and Controller rely on the Model. The Controller updates application data, which the View then displays. This separation allows for independent testing of the Model, enhancing maintainability. Various implementations of the MVC pattern are feasible, such as treating activities and fragments as controllers, responsible for data processing and updating views.

Implementation Approaches

  • Activities and Fragments as Controllers: Responsible for data processing and view updates.
  • Activities and Fragments as Views: Controllers and Models as separate classes without extending any Android class.

When Views adhere to the single responsibility principle-updating the Controller for each user event and displaying data from the Model without business logic-UI tests suffice to validate View functionalities.

Advantages of MVC Pattern

  • MVC enhances code testability and facilitates feature implementation by separating concerns.
  • Unit testing of Models and Controllers is feasible as they do not rely on Android classes.
  • View functionalities can be verified through UI tests when Views strictly adhere to the single responsibility principle.

MVC Pattern

  • MVC pattern enhances code testability and facilitates the implementation of new features by emphasizing the separation of concerns.
  • Unit testing of the Model and Controller is feasible since they do not extend or utilize any Android class.
  • The View's functionalities can be validated through UI tests if the View adheres to the single responsibility principle (updating the controller and displaying data from the model without incorporating domain logic).

Disadvantages:

  • Code layers are interdependent even when MVC is correctly implemented.
  • Lacks a mechanism to manage UI logic, such as how data should be displayed.

The Model-View-Presenter (MVP) Pattern

The MVP pattern represents the second evolution of Android app architecture and remains a recommended choice for aspiring developers. Each component serves a distinct purpose:

  • Model: This layer stores data, manages domain logic (real-world business rules), and handles communication with the database and network layers.
  • View: The UI (User Interface) layer that visualizes data and tracks user actions to inform the Presenter.
  • Presenter: Retrieves data from the model, applies UI logic to determine display content, manages the View's state, and responds to user input notifications from the View.
  • Model-View-Presenter (MVP) Pattern:
    • Model: This component pertains to the View layer, which is responsible for the visualization of data and tracking user actions to inform the Presenter.
    • Presenter: It retrieves data from the Model, applies UI logic to determine the display content, manages the View's state, and responds to user input notifications.
  • Contract:
    • No conceptual relationship exists in Android components.
    • Facilitates easy code maintenance and testing by separating the application's model, view, and presenter layers.
    • If the developer neglects the single responsibility principle, the Presenter layer may evolve into a monolithic class handling various responsibilities.
  • The Model-View-ViewModel (MVVM) Pattern

    The MVVM pattern, the third iteration of Android architecture, was recommended alongside the release of Android Architecture Components. It comprises distinct layers:

    • Model: Handles data source abstraction and collaborates with the ViewModel for data retrieval and storage.
    • View: Informs the ViewModel about user actions.
    • ViewModel: Exposes relevant data streams to the View.

Explanation of MVVM Architecture Pattern

  • Model: This layer serves to abstract data sources. It collaborates with ViewModel to retrieve and store data.
  • View: This layer communicates user actions to the ViewModel.
  • ViewModel: It exposes relevant data streams to the View.

ViewModel in Detail

  • The MVVM and MVP patterns share similarities as both effectively abstract the state and behavior of the View layer. In MVVM, Views can bind themselves to data streams exposed by the ViewModel.
  • ViewModel in Detail
  • In the MVVM schema, the View notifies the ViewModel about various actions. The View holds a reference to the ViewModel, while the ViewModel remains unaware of the View. This creates a many-to-one relationship between the View and ViewModel, supporting two-way data binding.

Comparison of MVC, MVP, and MVVM Architecture Patterns

PatternDependency on Android APIXML ComplexityUnit TestabilityFollow Modular and Single Responsibility Principle
MVCHighLowDifficultNo
MVPLowLowGoodYes
MVVMLow or No dependencyMedium to HighBestYes

Advantages of MVVM Architecture

  • Developers can create applications that are adaptable to future changes.
  • Offers a modular design, ensuring thorough testing and code maintenance.

Advantages of Architecture

  • Developers have the flexibility to design applications that can accommodate future changes easily.
  • Adopting an architectural approach provides a modular design for applications, ensuring better quality testing and easier maintenance of code.

Disadvantages of Architecture

  • Implementing a project entirely in an architectural pattern can be a time-consuming process.
  • Strict discipline is crucial from the development team to prevent inadvertent changes that could compromise the architecture's integrity.

Please Login to Comment...

In order to participate, please log in using the form below:

LoginLike
The document Android Architecture Patterns is a part of Software Development category.
All you need of Software Development at this link: Software Development
Download as PDF

Top Courses for Software Development

Related Searches
Extra Questions, Android Architecture Patterns, Android Architecture Patterns, shortcuts and tricks, study material, Free, Semester Notes, practice quizzes, Viva Questions, Previous Year Questions with Solutions, Android Architecture Patterns, pdf , past year papers, Important questions, Summary, Objective type Questions, video lectures, MCQs, mock tests for examination, Sample Paper, Exam, ppt;