0

For example, I have a GUI like this: enter image description here

So, I have a lot of panels. My GUI is rather functional, so it is very big. I don't want to create all GUI in one class. So, I can separate left panel, center panel and bottom panel in individaul classes. But they need to have access to some shared data. So, I could create class MainView which will store shared data and draw panels where they should be. After that, I could create classes LeftPanelView, CenterPanelView, BottomPanelView, that should extend MainView.

For all these classes I should have a ViewModel. For each View I should have its own ViewModel. So, I should have MainViewModel, LeftViewModel, CenterViewModel, BottomViewModel. And, as well as hierarchy of view view models will be connected in hierarchy. LeftViewModel, CenterViewModel and BottomViewModel will extend MainViewModel.

I have no doubt that View models have to be connected in hierarchy. But I do not know whether this is good to connect view classes in hierarchy. Prompt me please. Maybe, it is an awful design and it will be better to develope all gui in one class?

  • 1
    What about using Composition instead of inheritance? Each of bottom, center, top, left and right view models can have an associated MainViewModel instance to access/modify the shared model. – dic19 Apr 17 '16 at 19:50
  • @dic19, and in MainViewModel I could get getters. Am I right? – Александр Елизаров Apr 17 '16 at 20:07
  • 1
    Use one of the common Swing observer pattern approaches, examined [here](http://stackoverflow.com/a/3072979/230513); use `Action` to encapsulate/export functionality, as shown [here](http://stackoverflow.com/a/4039359/230513). – trashgod Apr 17 '16 at 20:28
  • 1
    I agree with @trashgod, observer pattern (events and listeners) are more common in Swing than plain getters and setters. – dic19 Apr 17 '16 at 20:42
  • @trashgod, can you explain me, please? Why action is good in MVVM? In MVVM we have to have small action listeners which only calls methods bind() and backBind(). – Александр Елизаров Apr 17 '16 at 20:42
  • @dic19, but if I'm going to use observer or Actions with functionality it will be not a MVVM. Where am I wrong? – Александр Елизаров Apr 17 '16 at 20:46
  • @АлександрЕлизаров: MVVM et al. are _not_ the panacea for which you're likely hoping. Instead, recognize and use known [patterns](http://stackoverflow.com/q/1673841/230513) to solve specific design problems. – trashgod Apr 17 '16 at 20:55

0 Answers0