1

I have read a number of articles/explanations on implementing MVC (Model View Controller) design pattern with Java such as:

http://www.leepoint.net/GUI/structure/40mvc.html

GUI not working after rewriting to MVC

and have visited several few more (including those in SO).

I am clear on what was explained in those links. However how should we implement a MVC when we have several JPanels/JFrames? So let say I want to implement a program which may display several JFrames at the same time.

Question: Should we try to only have 3 classes (Model, View, Controller) despite having multiple JFrames or should we have a separate View class for each JFrame?

I am aware that we could use the observer pattern to let various Frames communicate.


As for this link from SO, there is only 1 solution which does not address my question above: Java MVC multiple JFrame

I am also aware of The Use of Multiple JFrames: Good or Bad Practice?. But let's assume I will be having multiple JFrames which may display at the same time. I am more concern how would it be implemented with MVC.

Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106

1 Answers1

1

Should we try to only have three classes (Model, View, Controller) despite having multiple JFrames or should we have a separate View class for each JFrame?

The pattern doesn't specify only three classes; it describes the interaction among three instances. As discussed here, the pattern may appear an arbitrary number of times in a Swing application. It is a design element, not a panacea. Any of the observer pattern implementations mentioned here would serve. If you have a compelling reason for multiple JFrame instances, consider using modeless dialogs, each having an Observable delegate, to group related view components.

Is it alright if I create more than one class which represents view, e.g. five classes which act as view?

Yes, a Swing application may include multiple views. Swing's separable model architecture provides multiple view components, each of which listens to its own model and may serve as its own controller. For example, JTable is a view component that listens to its TableModel, but it also functions as a controller to handle editing within a cell. Moreover, as shown here and here, more than one view may listen to a single model.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • "The pattern doesn't specify only three classes" So is it alright if I create more than one class which represents view (for e.g. 5 classes which act as view)? – user3437460 Aug 27 '15 at 16:51