Java Android 上的 MVC 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2925054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:33:34  来源:igfitidea点击:

MVC pattern on Android

javaandroiddesign-patternsmodel-view-controller

提问by Mohit Deshpande

Is it possible to implement the model–view–controller pattern in Java for Android?

是否可以在 Java 中为 Android 实现模型-视图-控制器模式?

Or is it already implemented through Activities? Or is there a better way to implement the MVC pattern for Android?

还是已经通过活动实施了?或者有没有更好的方法来为 Android 实现 MVC 模式?

采纳答案by Pentium10

In Android you don't have MVC, but you have the following:

在 Android 中,您没有 MVC,但您有以下内容:

  • You define your user interfacein various XML files by resolution, hardware, etc.
  • You define your resourcesin various XML files by locale, etc.
  • You extend clases like ListActivity, TabActivityand make use of the XML file by inflaters.
  • You can create as many classes as you wish for your business logic.
  • A lot of Utilshave been already written for you - DatabaseUtils, Html.
  • 您可以通过分辨率、硬件等在各种 XML 文件中定义用户界面
  • 您可以按区域设置等在各种 XML 文件中定义资源
  • 你喜欢延长clases ListActivityTabActivity并利用由XML文件的inflaters
  • 您可以为业务逻辑创建任意数量的类。
  • 已经为您编写了很多Utils- DatabaseUtils、Html。

回答by Derick Bailey

The actions, views and activities on Android are the baked-in way of working with the Android UI and are an implementation of the model–view–viewmodel (MVVM) pattern, which is structurally similar (in the same family as) model–view–controller.

Android 上的操作、视图和活动是与 Android UI 一起使用的内置方式,并且是模型-视图-视图模型 (MVVM) 模式的实现,该模式在结构上与模型-视图相似(在同一家族中) -控制器。

To the best of my knowledge, there is no way to break out of this model. It can probably be done, but you would likely lose all the benefit that the existing model has and have to rewrite your own UI layer to make it work.

据我所知,没有办法打破这种模式。它可能可以完成,但您可能会失去现有模型所具有的所有好处,并且必须重写您自己的 UI 层以使其工作。

回答by RoflcoptrException

There is no single MVC pattern you could obey to. MVC just states more or less that you should not mingle data and view, so that e.g. views are responsible for holding data or classes which are processing data are directly affecting the view.

没有您可以遵守的单一 MVC 模式。MVC 只是或多或少声明你不应该混合数据和视图,例如视图负责保存数据或处理数据的类直接影响视图。

But nevertheless, the way Android deals with classes and resources, you're sometimes even forced to follow the MVC pattern. More complicated in my opinion are the activities which are responsible sometimes for the view, but nevertheless act as an controller in the same time.

但是,Android 处理类和资源的方式,有时甚至不得不遵循 MVC 模式。在我看来,更复杂的是有时负责视图但同时充当控制器的活动。

If you define your views and layouts in the XML files, load your resources from the res folder, and if you avoid more or less to mingle these things in your code, then you're anyway following an MVC pattern.

如果您在 XML 文件中定义视图和布局,从 res 文件夹加载资源,并且如果您或多或少地避免在代码中混合这些内容,那么您无论如何都遵循了 MVC 模式。

回答by Arunabh Das

Android UI creation using layouts, resources, activities and intents is an implementation of the MVC pattern. Please see the following link for more on this - http://www.cs.otago.ac.nz/cosc346/labs/COSC346-lab2.2up.pdf

使用布局、资源、活动和意图创建 Android UI 是 MVC 模式的实现。有关更多信息,请参阅以下链接 - http://www.cs.otago.ac.nz/cosc346/labs/COSC346-lab2.2up.pdf

mirror for the pdf

pdf的镜像

回答by Ramon Chan

There is no universally unique MVC pattern. MVC is a concept rather than a solid programming framework. You can implement your own MVC on any platform. As long as you stick to the following basic idea, you are implementing MVC:

没有普遍唯一的 MVC 模式。MVC 是一个概念,而不是一个可靠的编程框架。您可以在任何平台上实现自己的 MVC。只要你坚持以下基本思想,你就是在实现MVC:

  • Model:What to render
  • View:How to render
  • Controller:Events, user input
  • 模型:渲染什么
  • 视图:如何渲染
  • 控制器:事件、用户输入

Also think about it this way: When you program your model, the model should not need to worry about the rendering (or platform specific code). The model would say to the view, I don't care if your rendering is Android or iOS or Windows Phone, this is what I need you to render. The view would only handle the platform-specific rendering code.

也可以这样想:当您对模型进行编程时,模型不需要担心渲染(或特定于平台的代码)。模型会对视图说,我不在乎你的渲染是 Android 还是 iOS 还是 Windows Phone,这就是我需要你渲染的。该视图只会处理特定于平台的渲染代码。

This is particularly useful when you use Monoto share the model in order to develop cross-platform applications.

当您使用Mono共享模型以开发跨平台应用程序时,这尤其有用。

回答by typingduck

I agree with JDPeckham, and I believe that XML alone is not sufficient to implement the UI part of an application.

我同意 JDPeckham 的观点,我相信仅靠 XML 不足以实现应用程序的 UI 部分。

However, if you consider the Activity as part of the view then implementing MVC is quite straightforward. You can override Application(as returned by getApplication() in Activity) and it's here that you can create a controller that survives for the lifetime of your application.

但是,如果您将 Activity 视为视图的一部分,那么实现 MVC 非常简单。您可以覆盖Application(由 Activity 中的 getApplication() 返回),在这里您可以创建一个在应用程序的生命周期内存活的控制器。

(Alternatively you can use the singleton pattern as suggested by the Application documentation)

(或者,您可以按照应用程序文档的建议使用单例模式)

回答by Ben

Android's MVC pattern is (kind-of) implemented with their Adapterclasses. They replace a controller with an "adapter." The description for the adapter states:

Android 的 MVC 模式是用它们的Adapter类(有点)实现的。他们用“适配器”代替了控制器。适配器的说明指出:

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view.

Adapter 对象充当 AdapterView 和该视图的基础数据之间的桥梁。

I'm just looking into this for an Android application that reads from a database, so I don't know how well it works yet. However, it seems a little like Qt's Model-View-Delegate architecture, which they claim is a step up from a traditional MVC pattern. At least on the PC, Qt's pattern works fairly well.

我只是在研究一个从数据库读取的 Android 应用程序,所以我不知道它的工作情况如何。然而,它似乎有点像 Qt 的模型-视图-委托架构,他们声称这是对传统 MVC 模式的升级。至少在 PC 上,Qt 的模式运行良好。

回答by Hervé Donner

The best resource I found to implement MVC on Android is this post:

我发现在 Android 上实现 MVC 的最佳资源是这篇文章

I followed the same design for one of my projects, and it worked great. I am a beginner on Android, so I can't say that this is the best solution.

我为我的一个项目遵循了相同的设计,并且效果很好。我是 Android 的初学者,所以我不能说这是最好的解决方案。

I made one modification: I instantiated the model and the controller for each activity in the application class so that these are not recreated when the landscape-portrait mode changes.

我做了一个修改:我为应用程序类中的每个活动实例化了模型和控制器,以便在横向-纵向模式更改时不会重新创建它们。

回答by Edmund Chang

I think the most useful simplified explanation is here: http://www.cs.otago.ac.nz/cosc346/labs/COSC346-lab2.2up.pdf

我认为最有用的简化解释在这里:http: //www.cs.otago.ac.nz/cosc346/labs/COSC346-lab2.2up.pdf

From everything else I've seen and read here, implementing all these things makes it harder and does not fit in well with other parts of android.

从我在这里看到和阅读的所有其他内容来看,实现所有这些事情会使其变得更加困难,并且与 android 的其他部分不太匹配。

Having an activity implement other listeners is already the standard Android way. The most harmless way would be to add the Java Observer like the slides describe and group the onClick and other types of actions into functions that are still in the Activity.

让 Activity 实现其他侦听器已经是标准的 Android 方式。最无害的方法是像幻灯片一样添加 Java Observer,并将 onClick 和其他类型的操作分组到仍在 Activity 中的函数中。

The Android way is that the Activity does both. Fighting it doesn't really make extending or doing future coding any easier.

Android 的方式是 Activity 两者都做。与它作斗争并不会真正使扩展或进行未来的编码变得更容易。

I agree with the 2nd post. It's sort of already implemented, just not the way people are used to. Whether or not it's in the same file or not, there is separation already. There is no need to create extra separation to make it fit other languages and OSes.

我同意第二个帖子。它有点已经实施了,只是不像人们习惯的那样。不管它是否在同一个文件中,已经有分离了。无需创建额外的分离以使其适合其他语言和操作系统。

回答by 18446744073709551615

After some searching, the most reasonable answer is the following:

经过一番搜索,最合理的答案如下:

MVC is already implemented in Android as:

MVC 已经在 Android 中实现为:

  1. View = layout, resources and built-in classes like Buttonderived from android.view.View.
  2. Controller = Activity
  3. Model = the classes that implement the application logic
  1. 视图 = 布局、资源和内置类,如Buttonandroid.view.View.
  2. 控制器 = 活动
  3. 模型 = 实现应用程序逻辑的类

(This by the way implies no application domain logic in the activity.)

(顺便说一下,这意味着活动中没有应用程序域逻辑。)

The most reasonable thing for a small developer is to follow this pattern and not to try to do what Google decided not to do.

对于小型开发者来说,最合理的做法是遵循这种模式,而不是尝试去做 Google 决定不做的事情。

PS Note that Activity is sometimes restarted, so it's no place for model data (the easiest way to cause a restart is to omit android:configChanges="keyboardHidden|orientation"from the XML and turn your device).

PS请注意,Activity有时会重新启动,因此模型数据无处可去(导致重新启动的最简单方法是android:configChanges="keyboardHidden|orientation"从XML中省略并转动您的设备)。

EDIT

编辑

We may be talking about MVC, but it will be so to say FMVC, Framework--Model--View--Controller. The Framework(the Android OS) imposes its idea of component life cycle and related events, and in practice the Controller(Activity/Service/BroadcastReceiver) is first of all responsible for coping with these Framework-imposed events (such as onCreate()). Should user input be processed separately? Even if it should, you cannot separate it, user input events also come from Android.

我们可能在谈论MVC,但可以这么说FMVCFramework--Model--View--Controller。该框架(Android操作系统)规定其组件的生命周期和相关事件的想法,并在实践中的控制器Activity/ Service/ BroadcastReceiver)首先是负责为这些应对框架-imposed事件(如的onCreate() )。是否应该单独处理用户输入?即使它应该,你也不能分开它,用户输入事件也来自Android。

Anyway, the less code that is not Android-specific you put into your Activity/Service/BroadcastReceiver, the better.

总之,更少的代码,是不是Android的具体你把你的Activity/ Service/BroadcastReceiver越好。