简体   繁体   English

将Java swing MVC与Android设计模式进行比较

[英]Comparing Java swing MVC with Android design pattern

I'm doing a small research on design patterns in various platforms and I have prior experience in programming with Java. 我正在对各种平台的设计模式进行一项小型研究,并且我之前有过使用Java编程的经验。

While reading these posts: MVC pattern on Android and MVC architecture in Android , 在阅读这些帖子时: Android上的MVC模式和Android中的 MVC架构
I had an interesting question in mind: Why Java swing MVC can not be compared with Android development pattern? 我有一个有趣的问题:为什么Java swing MVC无法与Android开发模式进行比较? or Why we can't say that Android follows MVC? 或者为什么我们不能说Android跟随MVC? (in the context of overall "look and feel"). (在整体“外观和感觉”的背景下)。

In one answer, someone clarified MVC as: 在一个答案中,有人澄清MVC为:

  • Model: What to render 型号:渲染什么

  • View: How to render 视图:如何渲染

  • Controller: Events, user input 控制器:事件,用户输入

OK. 好。 well, now what I understand is: 好吧,现在我理解的是:

Java Swing MVC: Java Swing MVC:

  • In Java swing MVC, component class is an abstract class for all attributes in visual environment. 在Java swing MVC中, component类是可视化环境中所有属性的抽象类。 There is a distinct keyword called controls is used for some components such as buttons, lists etc. So, all controls and components are part of Model in MVC. 有一个名为controls的独特关键字用于某些components ,如按钮,列表等。因此,所有控件和组件都是MVC中Model的一部分。

  • Container inherits component . Container继承component and there are several LayoutManagers that defines layouts and place of components in container . 并且有几个LayoutManagers定义container布局和components位置。 Also there are Listeners have to be registered with according EventSources . 还有必须根据EventSources注册Listeners So, they all are the View in MVC. 所以,它们都是MVC中的View

  • Class that implements Listener interface methods in which we put our main logic and there are some EventClasses for each event. 实现Listener interface methods类,我们在其中放置主逻辑,每个事件都有一些EventClasses They all are part of Controller in MVC. 它们都是MVC中Controller的一部分。

putting all these examples together in an image; 将所有这些例子放在图像中; in swing MVC we have: 摇摆MVC我们有:

摇摆mvc

Android design pattern (visualizing as MVC): Android设计模式(可视化为MVC):

  • I think widgets are same as controls here. 我认为widgetscontrols相同。 Also, there are some other EventSources .They all act as a Model . 此外,还有一些其他的EventSources 。它们都充当模型

  • View package has viewgroups (that also contains several kinds of layouts .) and Listener interfaces . View包具有viewgroups (也包含几种layouts 。)和Listener interfaces they all are the part of View in MVC. 它们都是MVC中View的一部分。

  • Same as swing MVC, we can say Listener interface methods and activities are the part of controller . 与swing MVC相同,我们可以说Listener interface methods和活动是控制器的一部分。

putting all together in an image; 将所有图像放在一起; in Android we have: Android中我们有:

在此输入图像描述

As per above comparison, I consider following similarities : 根据以上比较,我考虑以下相似之处

  • Container - same as View Container - 与View相同

  • Layout managers - same as ViewGroup Layout managers - 与ViewGroup相同

  • Listeners - overall same in both architecture Listeners - 在两种架构中总体相同

  • controls - overall same as widgets controls - 总体上与widgets相同

  • Event delegation (registering appropriate listener with Event source and then implementing Listener's methods) - overall same in both architecture Event delegation (使用Event源注册适当的侦听器,然后实现Listener的方法) - 在两种架构中总体相同

So, can anyone explain which are the things that makes the Android design pattern different than Java swing MVC pattern? 那么,任何人都可以解释哪些因素使Android设计模式与Java swing MVC模式不同?
or If you believe that both are different things (in the context of design patterns used for development), then explain why? 或者如果您认为两者都是不同的东西(在用于开发的设计模式的背景下),那么解释原因?

Each Swing JComponent has ComponentUI that is responsible for displaying a component. 每个Swing JComponent都有ComponentUI负责显示组件。 While JComponent has a paint method, only user derived classes use it directly - "standard" implementations very often just call the paint method of the attached UI instead. 虽然JComponent有一个paint方法,但只有用户派生类直接使用它 - “标准”实现通常只调用附加UI 的paint方法 This allows to plug-in various look and feel implementations very easily - a different look and feel just provides different ComponentUI's. 这允许非常容易地插入各种外观和感觉 - 不同的外观和感觉只提供不同的ComponentUI。 Very clearly component is the "model" and the UI is the "view". 非常清楚组件是“模型”,UI是“视图”。 And Android does not inherit this decoupling in very obvious way. Android并没有以非常明显的方式继承这种脱钩。 For instance, its TextView seems just painting drawables when a similar JLabel has UI . 例如,当类似的JLabel具有UI时,其TextView似乎只是绘制drawable

This is however not the only place where MVC approach is used, and for some other specific cases Android and Swing MVC are very similar. 然而,这不是唯一使用MVC方法的地方,而对于其他一些特定情况,Android和Swing MVC非常相似。 For instance, Android ListView has a model ( ListAdapter ) very much like Swing JList has ListModel . 例如,Android ListView有一个模型( ListAdapter )非常像Swing JListListModel In both cases the model provides data while the component itself provides possibilities to display. 在这两种情况下,模型都提供数据,而组件本身提供了显示的可能性。 The possible difference is that Swing has more components with such decoupled data and presentation. 可能的区别在于Swing具有更多具有这种解耦数据和表示的组件。 JTable and JTree have similar models when Android does not provide such components out of box. 当Android不提供开箱即用的组件时, JTableJTree具有类似的模型。 There is no tree at all, and TableLayout is a that is called GridLayout in Swing, not similar to JTable . 根本没有树, TableLayout在Swing中称为GridLayout ,与JTable

The general approach for invalidating and repainting is not much different and in both frameworks only dedicated master thread can touch components. 无效和重新绘制的一般方法没有太大区别,在两个框架中,只有专用主线程才能触及组件。 Also, event listeners (probably other group of "controllers") are very similar in Java and Android, all differences between them could probably be called "subtle" only. 此外,事件监听器(可能是其他组“控制器”)在Java和Android中非常相似,它们之间的所有差异可能只被称为“微妙”。

Containers and layout manages are also quite similar between Swing and Android, the main differences being that Swing has much more possible implementations of its LayoutManager to choose from than Androids ViewGroup (classes like DatePicker , while derived from ViewGroup , are not general purpose layout managers). Swing和Android之间的容器和布局管理也非常相似,主要区别在于Swing有更多可能的LayoutManager实现可供选择而不是Androids ViewGroup (类似DatePicker的类,虽然派生自ViewGroup ,但不是通用布局管理器) 。 Also, part of Android layout is encoded in XML while Swing is typically plain Java only. 此外,Android布局的一部分是用XML编码的,而Swing通常只是普通的Java。 If layout managers are also kind of "controllers" responding to events like resizing or reorientation, this part is very similarly done. 如果布局管理器也是响应调整大小或重新定向等事件的“控制器”,那么这部分也是非常相似的。 However I am not sure if layout managers are really "controllers" as they update more the view than the model. 但是我不确定布局管理器是否真的是“控制器”,因为它们更新视图而不是模型。

In general, Swing seems more optimized for the large, complex GUI that shows up on a large screen when Android is better suited for small screens with just a few visible components, large enough to be operated with fingers without stylus. 一般来说,当Android更适合只有几个可见组件的小屏幕时,Swing似乎更适合大屏幕上显示的大型复杂GUI,大小足以用手指操作而不用手写笔。

The key feature of MVC is the separation of concerns between the three components: MVC的关键特性是分离三个组件之间的关注点:

  • The Model is responsible for maintaining an internal representation of the data. 模型负责维护数据的内部表示。
  • The View is responsible for displaying that data to the user and allowing them to interact with it. View负责向用户显示该数据并允许他们与之交互。
  • The Controller is responsible for updating the Model in response to user interactions with the View and to ensure that the View reflects the current state of the Model. Controller负责更新模型以响应用户与View的交互,并确保View反映模型的当前状态。

Depending on how strict a definition of MVC you're applying, you could also specify constraints around decoupling of the components; 根据您正在应用的MVC定义的严格程度,您还可以指定围绕组件分离的约束; for example you could assert that the Model should have no knowledge of the View or the Controller. 例如,您可以断言模型应该不了解View或Controller。 However, in most cases it would probably be considered sufficient to show that the three components are reflected separately in the software and conform to the general responsibilities above. 但是,在大多数情况下,可能认为足以表明这三个组成部分在软件中单独反映并符合上述一般责任。

In terms of your mappings of MVC to Swing and Android, I think the main issue you're struggling with is that of identifying the Model. 关于你对MVC到Swing和Android的映射,我认为你正在努力解决的主要问题是识别模型。 The Model does not have to be a distinct type of component in the UI framework, it can just be a simple class or set of classes that encapsulate the data in question. Model不必是UI框架中不同类型的组件,它可以只是一个简单的类或一组类,用于封装有问题的数据。 So, where you have the Model mapping to widgets and controls, I would argue that's incorrect: the Model is simply whatever data is being represented to the user. 因此,在您将模型映射到小部件和控件的地方,我认为这是不正确的:模型只是向用户呈现的数据。 So, for Swing I would map the MVC components as follows: 所以,对于Swing,我会按如下方式映射MVC组件:

  • Model: Any domain classes that represent your data, for example com.example.Order . 模型:表示数据的任何域类,例如com.example.Order
  • View: The controls, containers and layout managers. 视图:控件,容器和布局管理器。
  • Controller: The event listener implementations that manipulate the Model and update the View. 控制器:操纵模型并更新视图的事件侦听器实现。

You could come up with a stricter MVC implementation in Swing, but the above is probably a reasonable mapping. 您可以在Swing中提出更严格的MVC实现,但上面的内容可能是合理的映射。

The Android UI framework is more restrictive than Swing because applications on mobile devices are more constrained and Google would prefer that they behave in a fairly consistent manner, hence the need for classes like Activity to represent a particular thing that the user needs to do, and the relatively tight coupling with an Activity and given View / ViewGroup . Android UI框架比Swing更具限制性,因为移动设备上的应用程序受到更多限制,而Google希望它们以相当一致的方式运行,因此需要像Activity这样的类来表示用户需要做的特定事情,以及与Activity和给定的View / ViewGroup相对紧密的耦合。

Even so, you could map the Android components as follows: 即使这样,您也可以按如下方式映射Android组件:

  • Model: Any domain classes that represent your data, for example com.example.Order . 模型:表示数据的任何域类,例如com.example.Order
  • View: The Views and ViewGroups 观点: ViewsViewGroups
  • Controller: The Activity , assuming that the event listener code for your View s are part of the Activity , which is often the case. Controller: Activity ,假设View的事件监听器代码是Activity一部分,通常就是这种情况。

Now, you could also argue that the above is actually more of a Model-View-Presenter pattern with the Activity playing the part of the Presenter, but given that MVP can be thought of as a more specific variety of MVC, that doesn't necessarily invalidate the MVC mapping. 现在,你也可以争辩说上面实际上更多的是一个模型 - 视图 - 展示者模式,其中Activity扮演了Presenter的一部分,但是考虑到MVP可以被认为是一种更具体的MVC,那不是必然使MVC映射无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM