Android Design Architectures
 When developers work on a real mobile application whose nature is dynamic and
will expand its features according to the user’s need, then it is not possible to write
core logic in activities or fragments.
 To structure the project’s code and to give it a modular design(separated code
parts), architecture patterns are applied to separate the concerns.
 The most popular android architectures used by developers are the following:
 MVC (Model — View — Controller)
 MVP (Model — View — Presenter)
 MVVM (Model — View — ViewModel)
 The main idea of all these patterns is to organize the project in a proper way so
that all the codes get covered in the Unit Testing.
 Moreover, it is very helpful in the maintenance of the software, to add and remove
features and developers can keep a track of various crucial logic parts.
1.Model View Controller(Standard Android)
 It is the "default" approach with layout files,Activities/Fragments acting as the
controller and Models used for data and persistence
 With this approach, Activities are in charge of processing the data and updating
the views.
 Activities act like a controller in MVC but with some extra responsibilities that
should be part of the view.
 The problem with this standard architecture is that Activities and Fragments can
become quite large and very difficult to test.
2.Model View Presenter(Model View Presenter)
 When using MVP, Activities and Fragments become part of the view layer and they
delegate most of the work to presenter objects.
 Each Activity has a matching presenter that handles all access to the model.
 The presenters also notify the Activities when the data is ready to display.
3.Model-View-ViewModel(Data-binding MVVM)
 ViewModels retrieve data from the model when requested from the view via the
Android data binding framework.
 With this pattern, Activities and Fragments become very lightweight.
 Moreover, writing unit tests becomes easier because the ViewModels are
decoupled from the view.
MVC-Model View Controller
 Developing an android application by applying a software architecture
pattern is always preferred by the developers.
 An architecture pattern gives modularity to the project files and assures that
all the codes get covered in Unit testing.
 It makes the task easy for developers to maintain the software and to expand
the features of the application 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 splitting the code into 3 components.
 While creating the class/file of the application, the developer must 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 UI(User Interface) layer that holds components that are visible on the screen.
 Moreover, it provides the visualization of the data stored in the Model and offers interaction to
the user.
 Controller:
 This component establishes the relationship between the View and the Model.
 It contains the core application logic and gets informed of the user’s behavior and updates the
Model as per the need.
 In spite of applying MVC schema to give a modular design to the
application, code layers do depend on each other.
 In this pattern, View and Controller both depend upon the Model.
 Multiple approaches are possible to apply the MVC pattern in the project:
 Approach 1: Activities and fragments can perform the role of Controller and
responsible for updating the View.
 Approach 2: Use activity or fragments as views and controller while Model will
be a separate class that does not extend any Android class.
 In MVC architecture, application data is updated by the controller and
View gets the data.
 Since the Model component is separated, it could be tested
independently of the UI.
 Further, if the View layer respects the single responsibility principle
then their role is just to update the Controller for every user event and just
display data from the Model, without implementing any business logic.
 In this case, UI tests should be enough to cover the functionalities of the
View.
Example of MVC Architecture
 To understand the implementation of the MVC architecture pattern more
clearly, here is a simple example of an android application.
 This application will have 3 buttons and each one of them displays the
count that how many times the user has clicked that particular button.
 To develop this application the code has been separated in the following
manner:
 Controller and View will be handled by the Activity. Whenever the user clicks
the buttons, activity directs the Model to handle the further operations. The
activity will act as an observer.
 The Model will be a separate class that contains the data to be displayed. The
operations on the data will be performed by functions of this class and after
updating the values of the data this Observable class notifies the
Observer(Activity) about the change.
Advantages of MVC architecture pattern
 MVC pattern increases the code testability and makes it easier to
implement new features as it highly supports the separation of concerns.
 Unit testing of Model and Controller is possible as they do not extend or
use any Android class.
 Functionalities of the View can be checked through UI tests if the View
respect the single responsibility principle(update controller and display
data from the model without implementing domain logic)
Disadvantages of MVC architecture pattern
 Code layers depend on each other even if MVC is applied
correctly.
 No parameter to handle UI logic i.e., how to display the data.
Model—View—Presenter(MVP) Pattern
 MVP pattern is the second iteration of Android app architecture.
 This pattern is widely accepted and is still recommended for
upcoming developers.
 The purpose of each component is easy to learn:
 Model: Layer for storing data. It is responsible for handling the domain
logic(real-world business rules) and communication with the database and
network layers.
 View: UI(User Interface) layer. It provides the visualization of the data and
keep a track of the user’s action in order to notify the Presenter.
 Presenter: Fetch the data from the model and applies the UI logic to
decide what to display. It manages the state of the View and takes actions
according to the user’s input notification from the View.
 In the MVP schema, View and Presenter are closely related and have a reference to
each other.
 To make the code readable and easier to understand, a Contract interface class is
used to define the Presenter and View relationship.
 The View is abstracted and has an interface in order to enable the Presenter for
Unit Testing.
 In the initial stages of Android development, learners do write codes in
such a manner that eventually creates a MainActivity class which contains
all the implementation logic(real-world business logic) of the application.
 This approach of app development leads to Android activity gets closely
coupled to both UI and the application data processing mechanism.
 Further, it causes difficulties in the maintenance and scaling of such
mobile applications.
 To avoid such problems in maintainability, readability, scalability, and
refactoring of applications, developers prefer to define well-separated
layers of code
 By applying software architecture patterns, one can organize the code of
the application to separate the concerns.
 MVP (Model — View — Presenter) architecture is one of the most
architecture patterns and is valid in organizing the project.
 Most of the core business logic resides in Controller. During the lifetime of an
application, this file grows bigger and it becomes difficult to maintain the code.
 Because of tightly-coupled UI and data access mechanisms, both Controller and
View layer falls in the same activity or fragment. This cause problem in making
changes in the features of the application.
 It becomes hard to carry out Unit testing of the different layer as most of the part
which are under testing needs Android SDK components.
 Key Points of MVP Architecture
 Communication between View-Presenter and Presenter-Model happens via an
interface(also called Contract).
 One Presenter class manages one View at a time i.e., there is a one-to-one
relationship between Presenter and View.
 Model and View class doesn’t have knowledge about each other’s existence.
 Advantages:
 No conceptual relationship in android components
 Easy code maintenance and testing as the application’s model, view, and presenter layer
are separated.
 Disadvantages:
 If the developer does not follow the single responsibility principle to break the code then
the Presenter layer tends to expand to a huge all-knowing class.
The Model—View—ViewModel (MVVM) Pattern
 The third iteration of android architecture is the MVVV pattern.
 While releasing the Android Architecture Components, the Android team
recommended this architecture pattern. Below are the separate code layers:
 Model: This layer is responsible for the abstraction of the data sources. Model and
ViewModel work together to get and save the data.
 View: The purpose of this layer is to inform the ViewModel about the user’s action.
 ViewModel: It exposes those data streams which are relevant to the View.
 The MVVM and MVP patterns are quite similar because both are efficient in
abstracting the state and behavior of the View layer.
 In MVVM, Views can bind itself to the data streams which are exposed by
ViewModel.
 In MVVM schema the View informs the ViewModel about various actions.
 The View has a reference to the ViewModel while ViewModel has no
information about the View.
 The many-to-one relationship that exists between View and ViewModel
and MVVM supports two-way data binding between both.
 Developers always prefer a clean and structured code for the projects.
 By organizing the codes according to a design pattern helps in the maintenance of
the software.
 By having knowledge of all crucial logic parts of the android application, it is easier
to add and remove app features.
 Further, design patterns also assure that all the codes get covered in Unit Testing
without the interference of other classes.
 Model — View — ViewModel (MVVM) is the industry-recognized software
architecture pattern that overcomes all drawbacks of MVP and MVC design
patterns.
 MVVM suggests separating the data presentation logic(Views or UI) from the core
business logic part of the application.
 MVVM pattern has some similarities with the MVP(Model — View —
Presenter) design pattern as the Presenter role is played by ViewModel.
 However, the drawbacks of the MVP pattern has been solved by MVVM in
the following ways:
 ViewModel does not hold any kind of reference to the View.
 Many to 1 relationship exist between View and ViewModel.
 No triggering methods to update the View.
Ways to Implement MVVM in the Project
 There are 2 ways to implement MVVM design pattern in Android projects:
 Using the DataBinding library released by Google
 Using any tool like RxJava for DataBinding.
 Data Binding:
 Google releases the Data Binding Library for Android that allows the developers to bind UI
components in the XML layouts with the application’s data repositories.
 This helps in minimizing the code of core application logic that binds with View.
 Further, Two – way Data Binding is done for binding the objects to the XML layouts so that
object and the layout both can send data to each other.
Syntax for the two way data binding is @={variable}
 Advantages of Architecture
 Developers can design applications that can accept changes in the future.
 Gives a modular design to the application which assures good quality testing and
maintenance of code.
 Disadvantages of Architecture
 Writing the whole project code in an architecture pattern is a time taking process.
 Strict discipline is required from the developer team side as one misplaced change can
ruin the integrity of the architecture.
Thank you

Android DesignArchitectures.pptx

  • 1.
  • 2.
     When developerswork on a real mobile application whose nature is dynamic and will expand its features according to the user’s need, then it is not possible to write core logic in activities or fragments.  To structure the project’s code and to give it a modular design(separated code parts), architecture patterns are applied to separate the concerns.  The most popular android architectures used by developers are the following:  MVC (Model — View — Controller)  MVP (Model — View — Presenter)  MVVM (Model — View — ViewModel)  The main idea of all these patterns is to organize the project in a proper way so that all the codes get covered in the Unit Testing.  Moreover, it is very helpful in the maintenance of the software, to add and remove features and developers can keep a track of various crucial logic parts.
  • 4.
    1.Model View Controller(StandardAndroid)  It is the "default" approach with layout files,Activities/Fragments acting as the controller and Models used for data and persistence  With this approach, Activities are in charge of processing the data and updating the views.  Activities act like a controller in MVC but with some extra responsibilities that should be part of the view.  The problem with this standard architecture is that Activities and Fragments can become quite large and very difficult to test.
  • 5.
    2.Model View Presenter(ModelView Presenter)  When using MVP, Activities and Fragments become part of the view layer and they delegate most of the work to presenter objects.  Each Activity has a matching presenter that handles all access to the model.  The presenters also notify the Activities when the data is ready to display.
  • 6.
    3.Model-View-ViewModel(Data-binding MVVM)  ViewModelsretrieve data from the model when requested from the view via the Android data binding framework.  With this pattern, Activities and Fragments become very lightweight.  Moreover, writing unit tests becomes easier because the ViewModels are decoupled from the view.
  • 7.
  • 8.
     Developing anandroid application by applying a software architecture pattern is always preferred by the developers.  An architecture pattern gives modularity to the project files and assures that all the codes get covered in Unit testing.  It makes the task easy for developers to maintain the software and to expand the features of the application in the future.  There are some architectures that are very popular among developers and one of them is the Model—View—Controller(MVC) Pattern.
  • 9.
     The MVCpattern suggests splitting the code into 3 components.  While creating the class/file of the application, the developer must 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 UI(User Interface) layer that holds components that are visible on the screen.  Moreover, it provides the visualization of the data stored in the Model and offers interaction to the user.  Controller:  This component establishes the relationship between the View and the Model.  It contains the core application logic and gets informed of the user’s behavior and updates the Model as per the need.
  • 11.
     In spiteof applying MVC schema to give a modular design to the application, code layers do depend on each other.  In this pattern, View and Controller both depend upon the Model.  Multiple approaches are possible to apply the MVC pattern in the project:  Approach 1: Activities and fragments can perform the role of Controller and responsible for updating the View.  Approach 2: Use activity or fragments as views and controller while Model will be a separate class that does not extend any Android class.
  • 12.
     In MVCarchitecture, application data is updated by the controller and View gets the data.  Since the Model component is separated, it could be tested independently of the UI.  Further, if the View layer respects the single responsibility principle then their role is just to update the Controller for every user event and just display data from the Model, without implementing any business logic.  In this case, UI tests should be enough to cover the functionalities of the View.
  • 13.
    Example of MVCArchitecture  To understand the implementation of the MVC architecture pattern more clearly, here is a simple example of an android application.  This application will have 3 buttons and each one of them displays the count that how many times the user has clicked that particular button.  To develop this application the code has been separated in the following manner:  Controller and View will be handled by the Activity. Whenever the user clicks the buttons, activity directs the Model to handle the further operations. The activity will act as an observer.  The Model will be a separate class that contains the data to be displayed. The operations on the data will be performed by functions of this class and after updating the values of the data this Observable class notifies the Observer(Activity) about the change.
  • 14.
    Advantages of MVCarchitecture pattern  MVC pattern increases the code testability and makes it easier to implement new features as it highly supports the separation of concerns.  Unit testing of Model and Controller is possible as they do not extend or use any Android class.  Functionalities of the View can be checked through UI tests if the View respect the single responsibility principle(update controller and display data from the model without implementing domain logic)
  • 15.
    Disadvantages of MVCarchitecture pattern  Code layers depend on each other even if MVC is applied correctly.  No parameter to handle UI logic i.e., how to display the data.
  • 16.
  • 17.
     MVP patternis the second iteration of Android app architecture.  This pattern is widely accepted and is still recommended for upcoming developers.  The purpose of each component is easy to learn:  Model: Layer for storing data. It is responsible for handling the domain logic(real-world business rules) and communication with the database and network layers.  View: UI(User Interface) layer. It provides the visualization of the data and keep a track of the user’s action in order to notify the Presenter.  Presenter: Fetch the data from the model and applies the UI logic to decide what to display. It manages the state of the View and takes actions according to the user’s input notification from the View.
  • 19.
     In theMVP schema, View and Presenter are closely related and have a reference to each other.  To make the code readable and easier to understand, a Contract interface class is used to define the Presenter and View relationship.  The View is abstracted and has an interface in order to enable the Presenter for Unit Testing.
  • 20.
     In theinitial stages of Android development, learners do write codes in such a manner that eventually creates a MainActivity class which contains all the implementation logic(real-world business logic) of the application.  This approach of app development leads to Android activity gets closely coupled to both UI and the application data processing mechanism.  Further, it causes difficulties in the maintenance and scaling of such mobile applications.  To avoid such problems in maintainability, readability, scalability, and refactoring of applications, developers prefer to define well-separated layers of code  By applying software architecture patterns, one can organize the code of the application to separate the concerns.  MVP (Model — View — Presenter) architecture is one of the most architecture patterns and is valid in organizing the project.
  • 21.
     Most ofthe core business logic resides in Controller. During the lifetime of an application, this file grows bigger and it becomes difficult to maintain the code.  Because of tightly-coupled UI and data access mechanisms, both Controller and View layer falls in the same activity or fragment. This cause problem in making changes in the features of the application.  It becomes hard to carry out Unit testing of the different layer as most of the part which are under testing needs Android SDK components.
  • 22.
     Key Pointsof MVP Architecture  Communication between View-Presenter and Presenter-Model happens via an interface(also called Contract).  One Presenter class manages one View at a time i.e., there is a one-to-one relationship between Presenter and View.  Model and View class doesn’t have knowledge about each other’s existence.
  • 23.
     Advantages:  Noconceptual relationship in android components  Easy code maintenance and testing as the application’s model, view, and presenter layer are separated.  Disadvantages:  If the developer does not follow the single responsibility principle to break the code then the Presenter layer tends to expand to a huge all-knowing class.
  • 24.
  • 25.
     The thirditeration of android architecture is the MVVV pattern.  While releasing the Android Architecture Components, the Android team recommended this architecture pattern. Below are the separate code layers:  Model: This layer is responsible for the abstraction of the data sources. Model and ViewModel work together to get and save the data.  View: The purpose of this layer is to inform the ViewModel about the user’s action.  ViewModel: It exposes those data streams which are relevant to the View.  The MVVM and MVP patterns are quite similar because both are efficient in abstracting the state and behavior of the View layer.  In MVVM, Views can bind itself to the data streams which are exposed by ViewModel.
  • 27.
     In MVVMschema the View informs the ViewModel about various actions.  The View has a reference to the ViewModel while ViewModel has no information about the View.  The many-to-one relationship that exists between View and ViewModel and MVVM supports two-way data binding between both.
  • 28.
     Developers alwaysprefer a clean and structured code for the projects.  By organizing the codes according to a design pattern helps in the maintenance of the software.  By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features.  Further, design patterns also assure that all the codes get covered in Unit Testing without the interference of other classes.  Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns.  MVVM suggests separating the data presentation logic(Views or UI) from the core business logic part of the application.
  • 29.
     MVVM patternhas some similarities with the MVP(Model — View — Presenter) design pattern as the Presenter role is played by ViewModel.  However, the drawbacks of the MVP pattern has been solved by MVVM in the following ways:  ViewModel does not hold any kind of reference to the View.  Many to 1 relationship exist between View and ViewModel.  No triggering methods to update the View.
  • 30.
    Ways to ImplementMVVM in the Project  There are 2 ways to implement MVVM design pattern in Android projects:  Using the DataBinding library released by Google  Using any tool like RxJava for DataBinding.  Data Binding:  Google releases the Data Binding Library for Android that allows the developers to bind UI components in the XML layouts with the application’s data repositories.  This helps in minimizing the code of core application logic that binds with View.  Further, Two – way Data Binding is done for binding the objects to the XML layouts so that object and the layout both can send data to each other. Syntax for the two way data binding is @={variable}
  • 31.
     Advantages ofArchitecture  Developers can design applications that can accept changes in the future.  Gives a modular design to the application which assures good quality testing and maintenance of code.  Disadvantages of Architecture  Writing the whole project code in an architecture pattern is a time taking process.  Strict discipline is required from the developer team side as one misplaced change can ruin the integrity of the architecture.
  • 32.