配置和模型视图
我正在编写的一个小型应用程序上使用模型视图模式。场景如下:模型维护一个目录列表,可以从中提取所需的数据。该视图有一个配置或设置对话框,用户可以在其中修改此目录列表(该对话框有一个 JList,除了添加和删除按钮之外还显示该列表)。
我需要社区的一些建议:视图需要将这些更改传达给模型。我首先想到向模型添加这些方法:addDirectory() 和removeDirectory()。但我试图限制视图可用于与模型通信和操作模型的方法(或通道)的数量。这有什么好的做法吗?谢谢。
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 can 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加一个中间层,它与模型和视图通信,通常称为控制器:)
然后视图可以使用 addButtonPressed() 和removeButtonPressed() 等方法调用控制器。然后,这些方法调用 addDirectory() 和 removeDirectory(),而视图不知道这一点。
由于您正在编写一个小型应用程序,因此添加额外的 MVC 内容可能会产生开销。
编辑:在此设置中,视图仅引用控制器而不引用模型。
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.