Hi I am reading this post https://news.realm.io/news/eric-maxwell-mvc-mvp-and-mvvm-on-android/ where they explained very well about mvc, mvp, mvvm. I undertood how mvp design pattern works.
I don't find any drawback in MVP over MVVM. As they suggested this is issue
Presenter Concerns -> Maintenance - Presenters, just like Controllers, are prone to collecting additional business logic, sprinkled in, over time. At some point, developers often find themselves with large unwieldy presenters that are difficult to break apart.
Can anyone explain what does it means with an example & how it can be resolved using MVVM ?
MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing. All my projects(written in Kotlin for Android app) are based on MVVM.
MVP uses different libraries to help users who use the architecture. MVVM follows a pattern and is forecasted to evolve as a common tool used by many. The user doesn't need to use a pattern while creating the application. Two architectures can be used in an application to make it more useful.
Key Differences between MVC and MVVM In MVC, the controller is the entry point to the Application, while in MVVM, the view is the entry point to the Application. MVC Model component can be tested separately from the user, while MVVM is easy for separate unit testing, and code is event-driven.
I'm a big advocate of MVP and haven't really tried MVVM. The drawback of the possibility of a Presenter
getting out of control is something I've had experience with but it can be mitigated.
In the example from the post the business logic will be relatively simple. There is likely only one model to deal with and not too much complex logic.
Let's think of a more complicated example though. Say you have an app that sells flowers. Once the user has chosen their bunch of flowers they get taken to the order options screen where they can:
Then add some domain requirements:
This might not be the best UX but putting that aside you now have a Presenter
that has to deal with many different Model
s (account, address, vases, orders) and can very quickly start taking on many responsibilities beyond simply telling the View
what to display and passing events to the Model
. This violates the Single Responsibility Principle. Also any time a class starts getting beyond 500 lines I start getting upset.
The solution is relatively simple though. You separate all your separate bits of logic out into UseCase
classes. I use a relatively simple base class as follows:
public abstract class UseCase<I, O> {
public static final int NO_STATUS = -1;
public Observable<Integer> getStatus() {
return Observable.just(NO_STATUS);
}
public abstract Observable<O> getAction(I input);
}
You specify an input & output type and inject all the models you need in the constructor in the concrete implementation class. The Presenter
takes events and input from the View
passes this to the appropriate UseCase
, this then does the complex logic with the Model
and returns the appropriate data to the Presenter to update the View
.
You can send periodic status updates back to your Presenter
using the status if needed to update the UI state.
This way your Presenter
returns to being a simple conduit for transferring data & events between the View
and the Model
and the business logic is nicely contained in a separate class for each action.
As in the MVVP introduction in the article said:
MVVM with Data Binding on Android has the benefits of easier testing and modularity, while also reducing the amount of glue code that we have to write to connect the view + model.
Main differences of MVP and MVVP are:
However, many developers prefer MVP because they do not want some business logic codes being part of XML layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With