MVC中的类之间传递消息的正确方法是什么? [英] What is the correct way of Message Passing between Classes in MVC?

查看:148
本文介绍了MVC中的类之间传递消息的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Model {
    private SwingPropertyChangeSupport spcs;
    public void updatedb();
    public void addPropertyChangeListener(PropertyChangeListener listener)
}
public void Gui {
    private JFrame view;
    Gui() {
       view = new JFrame();  
       init();// for initilizing ui components
       EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    view.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });  
    }
    void addListener(ActionListener a);
    void init();
    void updateGUI();
}

public void Controller implements ActionListener,PropertyChangeListener {
     Model m;
     Gui g;
     public void PropertyChange(PropertyChangeEvent e);
     public void actionPerformed(ActionEvent e)
     Controller(Model m,Gui g){}

}




  1. 在3个课程之间传递消息的方式是什么? MVC消息传递图像如何扩展它为多个控制器和视图我已经阅读了SO答案 MVC模式和SWING 说这个实现效率不高?

  1. What is the correct of way of passing messages between 3 classes MVC message passsing Image How to extend it for multiple controllers and views?I have read SO answers The MVC pattern and SWING saying this implementation is not efficient?

是否正确制作Controller ActionListener和PropertyChangeListener?当我在actionperformed()中调用updatedb()时,它会使GUI缓慢吗?我还看到关于View具有将gui操作映射到模型操作的Controller的参考。这种方式更有效吗?

And whether it is right to make Controller ActionListener and PropertyChangeListener?Will it make GUI sluggish when I call updatedb() inside actionperformed()?I also read about View having reference of Controller which maps gui actions to model actions. Is this way more efficient?

哪些部分的代码应该在EventQueue.invokeLater里面?将init()函数放在run()之前是正确的吗?还是应该将整个gui类包装在run中?

Which part of code should be inside EventQueue.invokeLater? Is it right to put init() function outside run()?Or Should I wrap the whole gui class inside run?



< hr>


EventQueue.invokeLater(new Runnable() {

    public void run() {
        try {
            Gui view = new Gui();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});


推荐答案


什么是正确的在3类之间传递消息的方式MVC消息传递映像如何扩展它为多个控制器和视图我已经读过SO答案MVC模式和SWING说这个实现是不高效的

What is the correct of way of passing messages between 3 classes MVC message passsing Image How to extend it for multiple controllers and views?I have read SO answers The MVC pattern and SWING saying this implementation is not efficient?

根据需要,我个人定义每个级别的合同/接口,并提供每个子级别的引用。也就是说,模型和控制器有一个每个必须符合的合同,视图和控制器有一个每个必须满足的合同,以促进沟通。

This comes down to needs, personally, I define contracts/interfaces for each level and provide references to each sub level. That is, the model and controller have a contract that each must meet, the view and the controller have a contract that each must meet in order to facilitate communication.

这允许对于一个一对一的沟通渠道,它也将每个层分离开,因为他们不在乎如何实现另一个。

This allows for a one-to-one communication pipeline, it also decouples each layer in such away as they don't care about how the other is implemented.

同样,你可以使用某种类型的观察者模式,允许有兴趣的方面向对象注册自己他们可以在发生某些变化时通知。通常,这会为许多观察者创建一个可观察对象的单向通信。

Equally, you could use some kind of Observer Pattern, which allows interested parties to register themselves to an object so that they can notified when some change occurs. Typically this creates a one-way communication out of the observable object to many observers.


以及是否正确使Controller ActionListener和PropertyChangeListener ?

And whether it is right to make Controller ActionListener and PropertyChangeListener?

个人而言,控制器应该尽可能少地了解UI的物理实现方式。如果您的浏览器合同提供 ActionListener 和/或 PropertyChangeListener 支持,那没关系,但是如果您依赖于实现对象,这可能不是一个好主意(IMHO) - 它增加了耦合。

Personally, the controller should have as little knowledge about how the UI is physically implemented as possible. If your viewer contract provides ActionListener and/or PropertyChangeListener support, that's okay, but if you're relying on the underlying implementation objects, that's probably not a good idea (IMHO) - it increases the coupling.


当我调用updatedb时,它会使GUI迟钝)in actionperformed()?

Will it make GUI sluggish when I call updatedb() inside actionperformed()?

在GUI主线程的上下文中,您采取的任何操作(Swing中的事件调度线程示例),将阻止UI响应其事件队列中的新事件。阻止GUI主线程运行的时间越长,您的用户界面的响应度就越低。简短的答案,不要阻止GUI的主线程长时间运行或阻止进程...

Any action you take while within the context of the GUI's main thread (the Event Dispatching Thread in Swing for example), will prevent the UI from responding to new events within its event queue. The longer you prevent the GUI's main thread from running, the less responsive your UI will be. Short answer, don't block the GUI's main thread with long running or blocking processes...


我还阅读关于View有参考控制器将gui操作映射到模型操作。这种方式更有效吗?

I also read about View having reference of Controller which maps gui actions to model actions. Is this way more efficient?

一般来说,视图和模型实际上不应该相互交谈,他们假设要沟通通过控制器。视图和控制器需要彼此了解,模型和控制器需要了解每个。

Generally speaking the view and model shouldn't actually talk with each other, they're suppose to communicate via the controller. The view and controller need to know about each other and the model and controller need to know about each.

知识量将取决于合同的要求,例如,如果您使用观察者模式,则可观察对象不需要知道关于观察者的任何事情(除了他们有一种或多种他们可以调用的方法的事实)

The amount of knowledge will depend on the requirements of the contract, for example, if you use an Observer Pattern, the observable object won't need to know anything about the observer (other then the fact that they have one or more methods that they can call)


哪部分代码应该在里面EventQueue.invokeLater?将init()函数放在run()之前是正确的吗?还是应该将整个gui类包装在run中?

Which part of code should be inside EventQueue.invokeLater? Is it right to put init() function outside run()?Or Should I wrap the whole gui class inside run?

任何代码创建或修改UI或UI组件必须从GUI主线程的上下文中运行。

Any code that creates or modifies the UI or UI components MUST be run from within the context of the GUIs main thread.

您还可以查看在Java Swing中实现MVC的Controller部分 Java和GUI - ActionListeners根据MVC模式?,以便更多关于MVC在Swing中的讨论...

You could also take a look at Implementing the Controller part of MVC in Java Swing and Java and GUI - Where do ActionListeners belong according to MVC pattern? for more discussions on MVC in Swing...

这篇关于MVC中的类之间传递消息的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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