本文介绍了将MVP模式应用到JDialogs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Swing应用程序,并进一步已经使用模式将用户界面与业务逻辑分开。



当我的应用程序启动时,它执行以下代码:



<$模型模型= new BasicModel();
演示者Presenter = new Presenter(model);
View view = new SwingView(Presenter);
presenter.setView(view);

presenter.init();

创建用户界面。事件由 View 生成,并委托给 Presenter Presenter 然后操纵模型并相应地更新 View



为了处理一些事件,我需要从用户那里获取更多的信息。在这些事件的情况下,我认为适合于Swing视图生成一个新的 JDialog 窗口。



一行思维让我觉得这可能是orignal Presenter 中的适当代码:

  public void handlePreferences(){
Preferences prefs = view.getPreferences();
model.setPreferences(prefs);
}

也就是说,每个 JDialog的内容应该代表一个不同的对象,应该从 View 中检索并在模型中更新。但是,这会留下一个问题:我创建一个新的模型来表示首选项对象和一个新的 Presenter JDialog 中的事件处理



在我看来在查看演示者模型例如,如果我想将UI改为使用JSF,那么强制我做很多工作,比较难以进行移植。



请随意添加评论为了澄清。

解决方案

虽然在您的情况下不需要嵌套设计模式并不罕见。根据其他答案:



模型

  - 包含所有真实数据,变量,对象>
  - 知道如何将其存储的数据值设置为新值

  - 响应订单(方法调用)

  - 具有方法setPreferences (value1,value2,value3 ...);



查看

  - 是IO对于应用程序,只需输出和输入

  - 它只能在其自身的状态下工作

  - 它维护局部变量和对象,例如。它有JButtons,JMenus,int counter ...

  - 它知道如何通知状态变更主持人

  - 其状态对Presenter可见,或通过方法调用显示

  - 响应订单(方法调用)

  - 知道如何从用户获取偏好

  - 有方法askForPrefs();

  - 有方法getPrefState();



演示者 >
  - 对状态变更作出回应

  - 所有的决定,它告诉其他对象该怎么做(不怎么做)

  - 知道何时需要首选

  - 知道在哪里获得新的偏好以及在哪里放置它们

  - 有方法newPrefsAvailable();

演示者 - 检查模型,确定新的偏好是必需的

Presenter - this.myView.askForPrefs(); //告诉用户查询pref值

View.askForPrefs - 弹出一个JDialog框,retVals存储在视图中作为状态更改

View - this.myPresenter.newPrefsAvailable ();

演示者 - 回复this.myModel.setPreferences(this.myView.getPrefState());

Model.setPreferences - 将存储的值更改为View.getPrefState()

演示者 - 检查模型 - 确定偏好是好的

演示者 - 继续



JDialog被处理作为View的扩展,它就像一个JButton一样是View的成员。
模型具有权威的实际偏好值,视图具有表示JDialog状态的局部变量。


I'm writing a Swing application, and further to my previous question, have settled on using the Model-View-Presenter pattern to separate the user interface from the business logic.

When my application starts up, it executes the following code:

Model model = new BasicModel();
Presenter presenter = new Presenter(model);
View view = new SwingView(presenter);
presenter.setView(view);

presenter.init();

which creates the user interface. Events are generated by the View, and delegated to the Presenter. The Presenter then manipulates the Model and updates the View accordingly.

In order to handle some events, I need to obtain further information from the user. In the case of these events, I believe it is appropriate for the Swing view to spawn a new JDialog window.

One line of thinking makes me feel this might be appropriate code in the orignal Presenter:

public void handlePreferences() {
    Preferences prefs = view.getPreferences();
    model.setPreferences(prefs);
}

That is, the contents of each JDialog should represent a distinct object that should be retrieved from the View and updated in the Model. However, this leaves the question: do I create a new Model to represent the Preferences object and a new Presenter for event handling in that JDialog?

It seems to me that creating a new Presenter and Model internal to the original View forces me to do a lot of work that would be harder to port if I wanted to change the UI to use JSF, for example.

Please feel free to add comments for clarification.

解决方案

Although it is not uncommon to have "nested" design patterns it is not necessary in your case. Drawing on the other answers:

Model
 - Contains all the real data, variables, objects
 - knows how to set its stored data values to the new values
 - responds to orders (method calls)
 - has method setPreferences(value1,value2,value3...);

View
 - is the IO for the application, just output and input
 - it can only work on its self, on its state
 - it maintains local variables and objects, eg. it has JButtons, JMenus, int counters ...
 - it is knows how to inform the Presenter of State Change
 - its state is visible to the Presenter, or revealed by method call
 - responds to orders (method calls)
 - knows how to get preferences from the user
 - has method askForPrefs();
 - has method getPrefState();

Presenter
 - Responds to state changes
 - does all the deciding, it tells the other objects what to do (not how to do it)
 - knows when preferences are needed
 - knows where to get new preferences and where to put them
 - has method newPrefsAvailable();

Presenter - checks the Model, determines new preferences are required
Presenter - this.myView.askForPrefs(); //tells view to ask user for pref values
View.askForPrefs - pops up a JDialog box, retVals stored in the view as a state change
View - this.myPresenter.newPrefsAvailable();
Presenter - responds with this.myModel.setPreferences (this.myView.getPrefState());
Model.setPreferences - changes the stored values to View.getPrefState()
Presenter - checks the Model - determines preferences are good
Presenter - continues on

The JDialog is treated as just an extension of the View,it is a member of the View just like a JButton would be.The model has the authoritative actual preference values, and the view has local variables that represent the state of the JDialog.

这篇关于将MVP模式应用到JDialogs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:11