MVC vs MVP vs MVVM用例 [英] MVC vs MVP vs MVVM use cases

查看:100
本文介绍了MVC vs MVP vs MVVM用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发人员,我已经在我的应用程序中处理了所有这三种架构模式.我也经历了关于stackoverflow的几个帖子,关于每个帖子的区别.我的理解可能不是100%正确,但这是我到目前为止简要了解的内容.

  1. MVC-控制器接收用户输入.控制器更新模型,然后告诉视图进行更新.

  2. MVP-视图获取用户输入并通知Presenter. Presenter从模型获取数据,然后将其发送到View. Presenter和View具有一对一的关系.

  3. MVVM-View接收用户输入. ViewModel从Model生成数据,并输出任何订阅的View都可以使用该数据的数据流. View和ViewModel具有一对多关系.

问题在于,在访谈中,我多次被问到要问何时使用哪种模式的问题.我认为面试官想知道的是应用程序的类型(如银行,电子商务等)及其适当的体系结构模式.或至少对我为什么要在一个应用程序中使用MCV而在另一个应用程序中使用MVP以及为什么对MVVM使用某些具体解释.

我的研究做得很好,但是在互联网上找不到任何关于每种模式用例的正确答案.因此,要求请告诉我每个用例.

解决方案

据我所知:

MVC :模型视图控制器是Android开发的传统旧方法.在Android开发刚刚开始时就使用了它,并且已经使用了几年.这一次,唯一的知名模式是MVC.

因此,几乎所有旧应用程序都是从MVC开始的,但是随着代码库的增加,控制器(活动/片段)变得体积庞大,其中包含许多业务逻辑和网络请求以及异步任务.因此,由于这三个应用程序之间的高度依赖关系,它们很难维持加班时间,并且很难进行测试.

因此,如果您的应用程序很小,并且您不希望采用任何新的体系结构模式,或者对使用MVC的模式不了解0,但我强烈建议您此时不遵循MVC.

MVP ->由于MVC应用程序变得难以维护和测试,因此应用程序向MVP过渡.

Model View Presenter试图解决MVC问题,并将业务逻辑转移到Presenter.此处的演示者仅执行Interface动作,并且不知道要尝试更新的View.因此,由于演示者与控制器不相似,因此我们可以轻松地测试演示者和模型.因此,我们获得了测试和维护上的好处,但同时也带来了一个问题,即现在的演示者很聪明.他们最终开始变得笨重.造成了类似的问题.

在android中,应用也需要保持应用状态. MVC和MVP并非开箱即用,具有状态保存功能,您需要编写其他代码以进行状态维护.

另一方面,

MVVM 是最受欢迎的.模型视图viewModel是新的android体系结构.

您可以详细了解以下内容:

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

网络连接位于存储库中.因此,代码在您的片段或活动中要干净得多.这三个组件都易于测试和维护.

最大的优势之一就是您具有性能优势,因为ViewModel遵循单例模式,因此它具有开箱即用的状态保存机制,您可以通过使用ViewProviders并通过它创建实例来实现.

当您说什么应用程序应该使用什么时.如果应用程序规模庞大且复杂,则强烈建议使用MVVM,您还可以研究其他流行的体系结构组件,例如MVI和干净的体系结构(基于用例).我认为,应用程序产品类型不会改变体系结构要求.决定它的是复杂性和大小.您的安全要求会根据产品而有所不同.

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.

解决方案

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.

这篇关于MVC vs MVP vs MVVM用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆