简体   繁体   English

使用Swing实现Java SE MVC

[英]Java SE MVC implementation with Swing

I've implemented MVC pattern for Java SE with Swing using PropertyChageSupport and PropertyChageListener . 我使用PropertyChageSupportPropertyChageListener使用Swing为Java SE实现了MVC模式。 The diagram for implemented MVC is as follows. 实现的MVC的图如下。

修改过的MVC模式

In the implementation of View , I do property change in Model with the help of Controller . View的实现中,我在Controller的帮助下进行了Model属性更改。

View contains code like following for Ok button. View包含以下代码,如Ok按钮。

btnOk.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        modelController.setNumber(Integer.parseInt(numberField
            .getText()));
        modelController.setName(nameField.getText());
    }
});

Complete code can be found in SwingMVC . 完整的代码可以在SwingMVC中找到。

Now, My question is, Do I write above code for btnOk in View or Should I write it in a method in Controller so that in View , I'll be doing 现在,我的问题是, btnOkViewbtnOk编写上面的代码,或者我应该在Controller中的方法中编写它,以便在View ,我将会这样做

btnOk.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        modelController.btnOkActionPerformed();
    }
});

Of above two implementations, Which is the preferred way to implement MVC? 以上两个实现中,哪个是实现MVC的首选方式?

First a caveat: I'm not a professional or student but a hobbiest, but having said that, my own preference is with your second example, 首先要注意的是:我不是专业人士或学生,而是一个爱好者,但话虽如此,我自己的偏好是你的第二个例子,

btnOk.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        modelController.btnOkActionPerformed();
    }
});

The control would have to call methods on the view to extract information, and any methods it would call would be part of an interface that the view implements. 控件必须调用视图上的方法来提取信息,并且它将调用的任何方法都将是视图实现的接口的一部分。 My goal in this is to keep the view as dumb as possible and do almost anything to loosen coupling as much as possible. 我的目标是尽可能保持视野愚蠢,并做几乎任何事情以尽可能地松开耦合。

Your diagram suggests a model–view–presenter (MVP) pattern, which is compatible with Swing application design . 您的图表建议了一个模型 - 视图 - 演示者 (MVP)模式,它与Swing应用程序设计兼容。 In this context, Action is a convenient way to encapsulate application functionality for export from you model. 在此上下文中, Action是一种封装应用程序功能以便从模型导出的便捷方式。 As concrete examples: 作为具体例子:

  • DefaultEditorKit and StyledEditorKit export useful Action types that operate on the Document model common to text components. DefaultEditorKitStyledEditorKit导出对文本组件通用的Document模型进行操作的有用Action类型。 As shown in this example , such actions update the Document , which indirectly updates the corresponding view component. 如此示例所示,此类操作会更新Document ,间接更新相应的视图组件。

  • The ControlPanel in the example cited here exposes a number of Action instances that operate directly on an implicit model of List<Node> and List<Edge> . 此处引用的示例中的ControlPanel公开了许多Action实例,这些实例直接在List<Node>List<Edge>的隐式模型上运行。

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

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