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

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

问题描述

我是 android 开发人员,我在我的应用程序中研究了所有这三种架构模式.此外,我已经阅读了关于 stackoverflow 的几篇关于每个帖子的差异的帖子.我的理解可能不是 100% 正确,但这是我目前所知道的.

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 - 控制器接收用户输入.控制器更新模型,然后告诉视图更新自身.

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

MVP - View 获取用户输入并通知 Presenter.Presenter 从 Model 中获取数据,然后将其发送到 View.Presenter 和 View 是一一对应的关系.

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.

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

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.

问题是在面试中很多次我都被问到这个问题来告诉我什么时候使用哪种模式.我认为面试官想知道的是应用程序的类型(如银行、电子商务等)及其适当的架构模式.或者至少对为什么我想在一个应用程序中使用 MCV 而在另一个应用程序中使用 MVP 以及 MVVM 的一些具体解释.

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.

推荐答案

据我所知:

MVC:模型视图控制器是 Android 开发的传统旧方式.这是在 Android 开发刚开始时使用的,并且被狂热地使用了几年.这一次,唯一众所周知的模式是 MVC.

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.

因此,大多数旧应用程序都以 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.

因此,如果您的应用程序非常小,并且您不想遵循新的架构模式,或者对使用 MVC 的人的理解为 0,但我强烈建议此时不要遵循 MVC.

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 ->随着 MVC 应用程序变得难以维护和测试,应用程序向 MVP 过渡.

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

Model View Presenter 试图解决 MVC 的问题,并将表示和业务逻辑移至 Presenter.这里的演示者只执行界面操作,并不知道它试图更新的视图.因此,由于演示者与控制器不同,我们可以轻松测试演示者和模型.因此,我们获得了测试和维护的好处,但同时也产生了一个问题,即现在演示者是聪明的.它们最终开始变得笨重.创建一个类似的问题.

Model View Presenter tried to resolve the problems of MVC and moved presentation and 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.

同样在安卓中,应用程序需要维护应用程序状态.MVC 和 MVP 没有开箱即用的状态保存功能,您需要编写额外的代码来维护状态.

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 另一方面是最受欢迎的.模型视图 viewModel 是新的安卓架构.

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

你可以详细了解一下:

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.

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

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.

当您说应该使用什么应用程序时.如果应用程序规模庞大且复杂,强烈建议使用 MVVM,您还可以查看其他流行的架构组件,如 MVI 和干净的架构(基于用例).在我看来,应用产品类型不会改变架构要求.决定它的是复杂性和大小.您的安全要求因产品而异.

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天全站免登陆