MVC Architecture and Modal Dialog Windows

2019-07-01 18:53发布

问题:

I am developing a project in a MVC architecture. It should be a simple application to manage some customers.

There are MainModel, MainView and MainController classes which make the main window to show the content of the customers tables and to let the user insert, remove or edit customers.

My problem is that the insert and edit buttons should show some dialog windows to let the user insert and edit some text values and I have some doubts.

I would like to ask you some questions:

  • Should I use the MVC architecture for each dialog window?
  • If yes, I have tried doing it but my dialog windows are modal, so my code runs the model, runs the view but it gets blocked in the view and it doesn't run the controller class. How could I solve it?

    For example here it gets blocked in the "new InsertCustomerController..." instruction:

        CustomerModel customerModel = new CustomerModel();
        InsertCustomerView insertCustomerView = new insertCustomerView(customerModel);
        new InsertCustomerController(insertCustomerView, customerModel);
    

Thank you very much.

回答1:

Irrespective of modality, you can use the observer pattern to keep your dialogs synchronized with the application's model. This example uses a PropertyChangeListener; other approaches are mentioned here.



回答2:

Although I can't fully tell from your post, I'm not sure you're thinking of MVC the right way. But let's say you have a Customer, CustomerView, and CustomerController class..

Customer could contain all of the business logic related to being a customer-- so it might have methods like setBalance(int newBalance), getBalance(), etc.

The CustomerView class could essentially be a subclass of JPanel or JFrame (since it looks like you're using Swing from your question's tags). This class would be representative of one Customer instance. Maybe you could have a private Customer class variable. This class's responsibilities should only consist of displaying the data to the user contained in its Customer instance as well as allowing them to modify it.

It would be tough to say what the CustomerController would do since I don't know anything about your application, but it could potentially contain ActionListeners and that sort of thing that help to route input and output to the different parts of your model and view.

I did some googling and found a pretty straightforward example you may want to check out: http://www.austintek.com/mvc/

Good luck. Hope this helps.