Friday, August 28, 2020

MVVM (Model-View-View Model)

Design patterns and architecture of an application Plays a Key role in development.

Developers Have different types of architecture like MVVM, MVC and MVP.

In this blog post, we are going to deep dive about MVVM Architecture:

·         MVVM (Model-View-View Model) is one of the architectural patterns which enhances separation of layers,

·         it allows separating the user interface logic from the business (or the back end) logic.

·         Its target with other MVC patterns goal is to achieve the following

·         “Keeping UI code simple and free of app logic in order to make it easier to manage”.

MVVM has mainly the following layers:

Model

            Model represents the data and business logic of the app.The Model isn’t connected to the View. It includes the info, handling tools & business-logic.

The data can be retrieved from different sources, for example:

1.REST API

2.Realm Db

3.SQLite Db

4.Shared Preferences

5.Firebase

 

View: The view role in this pattern is to observe (or subscribe to) a View Model it takes

responsibility for displaying data. The view transmits some information to the View Model

and thereby updating user interface.

ViewModel: ViewModel provides a tool to transmit event to the Model that is observed by View. View and ViewModel

both are interconnected in both directions:

It is responsible for:

1.Exposing data

2.Handling visibility

3.Input validation

 

Let’s Look at Advantages of MVVM over other Patterns:

1.  In MVC, you have a triangular relationship between the components.

That is: The Controller owns the View and the Model.

The View relies on the definition of the Model.

The Model needs to fulfil the requirements of the View.

 

Model-view-Controller Pattern:

 

2.In MVVM, think of that triangle flattening out with each component only knowing about one other in the chain.

That is: View->ViewModel->Model

3.MVVM with data binding is attractive as it follows a more reactive programming model and produces less code.

4.Unit testing even easier, as there is no dependency on the View.

5.Using the official Google library that assures a proper generation of components;

6.Review at compilation stage;

7.No need for dozens of findViewById, setOnClickListener or similar code;

8.Possibility to write custom xml-attributes with the help of Binding adapters.

 

Model-view-ViewModel Pattern:

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment