I am using the Model-View pattern on a small application I'm writing. Here's the scenario: The model maintains a list of directories from where it c开发者_开发问答an extract the data that it needs. The View has a Configuration or a Setting dialog where the user can modify this list of directories (the dialog has a JList displaying the list in addition to add and remove buttons).
I need some advice from the community: The View needs to communicate these changes to the model. I thought first of adding to the model these methods: addDirectory() and removeDirectory(). But I am trying to limit the number of methods (or channels) that the View can use to communicate with and manipulate the model. Is there any good practice for this? Thank you.
Add an intermediate layer, which talks to both the model and the view, commonly known as a controller :)
The view can then call the controller with methods like addButtonPressed() and removeButtonPressed(). These methods then call addDirectory() and removeDirectory(), without the view knowing about this.
Since you're writing a small application, adding extra MVC stuff would probably be overhead.
EDIT: in this setup, the view only has a reference to the controller and not to the model.
精彩评论