3
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. 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?

  2. 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?

  3. 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?


EventQueue.invokeLater(new Runnable() {

    public void run() {
        try {
            Gui view = new Gui();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
Community
  • 1
  • 1
himanshu219
  • 654
  • 7
  • 22
  • 1
    There's several. You could create a series of contracts/interfaces which each level would need to implement, this would allow you to define, unfront, the expectations of each element, you could also use an [Observer Pattern](http://www.oodesign.com/observer-pattern.html) which would allow to provide event notification to interested parties as a coupld of ideas – MadProgrammer Nov 26 '14 at 04:02
  • But I am looking for ways which follows design principles - low coupling and high cohesion. – himanshu219 Nov 26 '14 at 04:12
  • Well neither of the suggest mechanisms have high coupling as they are relying on interfaces and not implementations, they provide a highly defined contract that each level is expected to honour and work within. The question you need to ask, is which one would be right in a given circumstance... – MadProgrammer Nov 26 '14 at 04:16

1 Answers1

4

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.

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

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.

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

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...

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)

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?

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

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...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I wanted to know whether making Controler actionlistener makes it run on which thread EVT or main thread? – himanshu219 Nov 26 '14 at 04:22
  • Can you please clearify which functions are called by EVT in this case say, if I enter a text using submit button because I read that ideally the setText() method and others that alter the component have to be called from the EDT itself.And whether if I will face any problem updating view using controller because EDT will be executing the actionperformed function at that time. – himanshu219 Nov 26 '14 at 08:45
  • Unless otherwise specified, it's reasonable to assume that any notification coming from the view is likely been executed within the context of the EDT. If your controller is reacting to these messages by executing a long running process or blocking call, then it will block the EDT... – MadProgrammer Nov 26 '14 at 08:56