我正在尝试开发一个“Person数据库”Java Swing应用程序,使用Observer/Observable使用MVC设计范例。以下是我正在使用的M/V/Cs的简化摘要:Java;多个MVC和Swing关系最佳实践?
App
AppModel
(Empty right now, possibly i'll store certain static application info such as version number here)
AppView
(Creates a JFrame and a few other Swing components)
AppController
(Instantiates AppModel, AppView and also a PersonController and a PersonListController)
Person
PersonModel
(Stores info for 1 person)
PersonView
(Displays a number of form fields inside a JPanel (i.e Name, Age, Phone number). Observes PersonModel.)
PersonController
(Instantiates PersonView. Observes PersonView. Instantiates PersonModel. Updates PersonModel.)
PersonList
PersonListModel
(Stores a list of Persons)
PersonListView
(Displays a list of persons with appropriate Add/Delete buttons. Observes PersonList.)
PersonListController
(Instantiates PersonListView. Observes PersonListView. Instantiates PersonListModel. Updates PersonListModel)
另外,应用程序启动的'引导程序'。它创建一个新的AppController。
在实际的应用程序中,会有更多(和不同的)模型/视图/控制器对象,但我希望这个例子保持简单。
我不明白我能如何将这些独立的视图合并到一个用户界面中,同时保持良好的分离关注点。
以PersonListView为例。恕我直言,它不需要关心的AppView(与JFrame等)。 PersonListView只需要查看其自己的型号并相应地更新本身。但是,我无法强制执行此操作,因为PersonListView自己的Swing组件需要添加到另一个视图的AppView的Swing组件中。
因此,目前AppController正在实例化自己的视图,并间接加入了一个PersonView和PersonListView(通过控制器的实例化)。 AppController然后为每个视图抓取'主'Jpanel,抓取它们应该添加到AppView上的'父'Swing组件,并添加它们。
这似乎并没有正确的方式来做到这一点。我将摆动相关的成员从他们的藏身处拖走,并在控制器内部与他们搞混了。实际上,在控制器中实例化模型和视图看起来不太好,但我无法找出更好的方法。
我最近看到足够的'简单的MVC'教程,我梦想着血腥的事情 - 但似乎没有一篇教程涉及多个模型,视图,控制器的关系,特别是涉及Swing的关系。也许我错了,应用程序应该只有一个视图?也许我需要一个'关系'类,它需要每一个模型/视图/控制器并适当地实例化东西?
任何意见,将不胜感激,因为我完全茫然!
感谢您的意见,我将尝试像您所建议的那样实施组控制器。我想我会让它在程序中实例化所有MVC,然后根据需要将每个视图的Swing组件连接在一起。 – user2359111 2013-05-08 13:38:59