MVC vs MVP vs MVVM use cases

混江龙づ霸主 提交于 2020-01-13 07:03:50

问题


I am and android developer and I have worked on all of these three architecture patterns in my applications. Also I have gone through several post's on stackoverflow about the difference of each. My understanding might not be 100% correct but this is what I know so far in brief.

  1. MVC - User input is received by controller. Controller updates the model then tells the view to update itself.

  2. MVP - View gets the user input and notify the Presenter. Presenter gets the data from Model and then sends it to View. Presenter and View have one-to-one relation.

  3. MVVM - User input is received by View. ViewModel generates the data from Model and puts out a stream of data any View subscribed to it can consume that data. View and ViewModel have one-to-many relation.

The problem is that many times in interviews I have been asked the question to tell which pattern to use when. What I think the interviewer wants to know is the type of application (like banking, e-commerce, etc) and their appropriate architecture pattern. Or at least some concrete explanation as to why I would like to use MCV in one application and MVP in another and so for MVVM.

I did my research well but could not found any proper answer on the internet that talks about the use case of each pattern. Thus, request to please tell me use case for each.


回答1:


From my knowledge:

MVC: Model View Controller is the traditional old way of Android development. This was used when Android development just started and was avidly used for few years. In this time, the only well known pattern was MVC.

So, mostly all old applications started as MVC but as the code base increases, the controller(Activities/Fragments) become bulky with lot of business logic and network requests and async tasks living in them. So, such applications become difficult to maintain overtime and are very harder to test due to the high dependency between the three.

So, if your application is very small and you want to follow no new architecture patterns or have 0 understanding of those use MVC but I highly recommend not to follow MVC in this time.

MVP -> As MVC applications become difficult to maintain and test, applications transitioned towards MVP.

Model View Presenter tried to resolve the problems of MVC and moved business logic to presenters. The presenter here just performs the Interface actions and has no knowledge of the Views it is trying to update. So, as presenters are not similar to controllers we can easily test presenters and models. So, we achieve testing and maintenance benefits but it also creates a problem that now presenters are the smart one. They start becoming bulky eventually. Creating a similar problem.

Also in android, apps need to maintain app state. MVC and MVP don't come out of the box to have state saving capabilities, you need to write additional code for state maintenance.

MVVM on the other hand is most popular. Model view viewModel is the new android architecture.

You can go in detail and learn this:

https://developer.android.com/jetpack/docs/guide

Network connections live in repository. So, code is much cleaner in your fragments or activities. All three components are easily tested and maintainable.

One of the biggest advantages is you have performance benefits as it does the state saving mechanism out of the box as ViewModel follows the singleton pattern and you can achieve that by using ViewProviders and creating instance through it.

When you say what application should use what. If an application size is huge and complex would highly suggest MVVM and you can also look into other popular architecture components like MVI and clean architecture(Use case based) as well. In my opinion, an application product type doesn't change architecture requirements. It's the complexity and size that determines it. Your security requirements change based on the product.



来源:https://stackoverflow.com/questions/54106509/mvc-vs-mvp-vs-mvvm-use-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!