[英]Using the Model-View-Presenter design pattern for a Java Swing application
I'm working on developing a Java application for organising personal music collections that allows the user to search their digital music library with the help of textual lists displayed in a table, choose songs for playback and provide information about them ie something like Rekordbox ( https://rekordbox.com/en/ ). 我正在开发用于组织个人音乐收藏的Java应用程序,该应用程序允许用户借助表中显示的文本列表搜索其数字音乐库,选择要播放的歌曲并提供有关它们的信息,例如Rekordbox( https ://rekordbox.com/en/ )。
After conducting some research on how to design and implement such a system, I came across the Model-View-Presenter design pattern and from what I understood it is a pattern that allows flexible , reusable and test driven code to be written. 在对如何设计和实现这样的系统进行了一些研究之后,我遇到了Model-View-Presenter设计模式,据我了解,该模式允许编写灵活,可重用和测试驱动的代码。
So to come to my problem: 所以来解决我的问题:
So, my question is how can I implement the Presenter so that the different views (which use different models) are being able to communicate and share information between each other? 因此,我的问题是我该如何实现Presenter,以便不同的视图(使用不同的模型)能够相互通信和共享信息? Should I have a single Presenter to which the views talk or have a presenter for every view?
我应该只有一个与该视图交谈的演示者,还是每个视图都有一个演示者? If it's possible to have a single presenter how can that be achieved?
如果有可能只有一个演示者,那该如何实现? If I have one Presenter for the MenuBarView, TablePanelView and PlayerPanelView and those views are contained in another view (which is the MainFrameView ) should I combine the presenters in some way, and if yes how?
如果我为MenuBarView,TablePanelView和PlayerPanelView有一个演示者,并且那些视图包含在另一个视图(即MainFrameView )中,则应该以某种方式组合演示者,如果可以,如何组合?
If i were you I would try the MVC (Model View Control) pattern before trying MVP. 如果我是您,我将在尝试MVP之前尝试使用MVC(模型视图控制)模式。 It´s very similar but I would say it´sa bit easier to understand.
它非常相似,但是我想说起来比较容易理解。
I wouldn´t create a own view for the MenuBar because you probably won´t create it dynamically. 我不会为MenuBar创建自己的视图,因为您可能不会动态创建它。 Just write a method in your MainFrameView where you initialize it and call that in the constructor of the MainView.
只需在MainFrameView中编写一个用于初始化它的方法,然后在MainView的构造函数中调用该方法即可。
The model is a property of the item you use. 模型是您使用的项目的属性。 Now if you want to create a Panel with a own model but also want to access the model from the MainFrameView you simply write a Getter/Setter for it.
现在,如果您要创建一个具有自己模型的面板,但又想从MainFrameView访问该模型,则只需为其编写一个Getter / Setter。 It looks like this in the MainFrameView:
在MainFrameView中看起来像这样:
public TablePanelView tpv;
public void initTablePanelView(TablePanelModel tpm){
tpv = new TablePanelView();
tpv.setModel(tpm);
}
So you can use the public methods getModel() or setModel() that you wrote in the TablePanelView to access the Model. 因此,您可以使用在TablePanelView中编写的公共方法getModel()或setModel()来访问模型。
I hope that helped. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.