0

I am developing an application in Java and I am using Swing for GUI. I was going to do it using Presentation Model pattern which is introduced by Martin Fowler here: http://martinfowler.com/eaaDev/PresentationModel.html

but I really don't like when I have to deal with View within my Presentation Model class so I figure out I can do it differently.

In my approach I will have a CustomViewModel class extending Swing component model ( like TableModel for JTable or ButtonModel for JButton). This class will override method that is executed when user updates data on model. So I will do my job like updating DomainModel and then calling parents method. This way I will have data always in sync between DomainModel and ViewModel.

For Example:

class MyCustomJTableModel extends TableModel{
   ... overriding required methods for synchronization
}

in setup gui function I would have:

JTable table = new JTable();
MyCustomJTableModel model = new MyCustomJTableModel();
model.setDataSource( Database.getDB().getModelFor( "model.name" ) );
table.setModel( model )
View view = new View();
view.addComponent( "JTableModel", model );

ResourceHandler rH = new ResourceHandler();
rH.add( model );

So now in my Controller function I can have:

public class FileOpenCOntroller{
    public void perform(ResourceHandler rH){

        MyCustomTableModel model = rH.getModel("JTableModel");
        model.loadDataFromDataSource( "File.xml" );
    }
}

And that's it MyCustomTableModel has overriden method that is used by JTable to update it's content and having my code before the actual call to parents setModel I am updating actual Model. No need for view reference. The problem is when I need to deal with JFileChooser which don't have similar relationship as rest of components. I am talking about some ViewModel class.

Does anyone have an idea how to tacle this approach.

What I am trying to achieve is not to have reference to View in both MyCustomComponentModel and Controller. I would like to make the comunication between controller and view via set of CustomComponentModel which extends view models like TableModel, ButtonModel etc. The purpose of that is easier TDD. In this approach only Layout of things is not tested.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • put any Items to util.List (by default required AbstractTableModel) or if Item returns Vector that you can to use, override DeafultTableModel, another issue will be (care about) lifecycle from underlaying array (Item) to Model – mKorbel Oct 09 '13 at 07:24
  • _deal with View within my Presentation Model_ that's definitely **not** what Fowler suggests, so you are misunderstanding the articles somewhere. So you are wasting your time in solving a "problem" that you introduced yourself ;-) – kleopatra Oct 09 '13 at 10:06
  • @kleopatra Fowler suggests in his article that either Presentation Model or View should deal with synchronization and there are two ways to do that. One is to have view reference in your Presentation Model which is better from TDD perspective. The Second one is to have View dealing with synchronization and that is something worse from TDD perspective. So what I am trying to achieve is to figure out a way of not having to deal with View mocks in my tests :) And if I can wrap component model in a class and comunicate with view via it I would achieve my goal. – user2861410 Oct 10 '13 at 06:44
  • be careful: the synch Fowler is talking about is the synch between PresentationModel and View (vs. between pm and domain/backend) And that's handled easily via standard mechanisms (plain beans or a binding framework like BeansBinding or JGoodies Binding), so testing of such synch can (mostly) rely on the binding framework having been tested extensively plus some trivialites that the bindings are indeed in place. – kleopatra Oct 10 '13 at 10:00
  • if you are interested, [My swingx incubator](https://java.net/projects/jdnc-incubator/sources/svn/show/trunk/src/kleopatra/java/org/jdesktop/appframework/beansbinding?rev=3385) has examples (untested ) of my usual setup, using Fowler's example. – kleopatra Oct 10 '13 at 10:01

0 Answers0