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.