tags:

views:

314

answers:

2

i've started on some basic java coding with gwt, and im getting a bit concerned about the heft of my main class.

For instance - how does one compartmentalize the keyhandlers, since they trigger a number of changes to the UI, how could i move this into a separate .class file and still be able to access all the various widgets in the main class, without having to pass everything to the handler (ie. all the widgets i manipulate after the click event).

i've googled but didnt come across any particularly good examples - know of any readily legible code-bases i could read to see how it should be done? (gwt's own tuts are pretty basic, and just kitchen-sink every thing into a single file)

thanks!

+4  A: 

I hate to say something so unimaginative, but MVC works--it's not the ultimate, but it can start getting you organized.

EDIT: While searching on a semi-related topic, I came across this which has similar ideas to mine but goes into more detail.

What that means in terms of GWT is that you should think of just laying out your GUI components in one class, put all your event handling in a second and put your object model objects separate from the other two.

One way to accomplish this is to make most or all the controls on your GUI public members. This sounds kind of lame, but their usage is encapsulated inside the controller so it's not like you have uncontrollable access--in fact your access is clearer/better defined than if all your members were private but your view code was combined with the controller.

Specific tricks:

Have listeners be their own class. You can often reuse them-- in other words, avoid anonymous inner classes. I sometimes create a listener class and instantiate a new instance for each button/control that needs to have a similar effect when pressed. If I need it to act slightly differently for a given button, I'll pass something into the constructor of the "special" handlers so that they know to act a little differently. You can also create different handler sub-classes if necessary--I'm just saying don't forget that event handlers are classes, you can use inheritance and everything if need be.

One Very Old GUI Trick I learned a long time ago, try not to have various mini-handlers modifying the GUI in different ways, instead have all the "active" buttons and controls set a state within your GUI and then call a single method that applies that state to ALL the controls on your GUI. When you get beyond a trivial GUI this can be a life-saver. If I'm not being clear, leave a comment and I'll leave an example for you.

Property sheets:

There is a special case for GUIs--the property sheet style GUI. I've done a LOT of these and they are irritating as HELL. They tend to have dozens or hundreds of controls on them, each GUI control tends to be tied to a specific field in your model and there are just hundreds of lines of copy and paste boilerplate code connecting them, each group copied and pasted with a few items changed--at minimum it's like 3 lines of code per control (Create control, copy value in and copy value out).

I always write these with a "Smart" controller--one that can intelligently bind the control to some data without any unique code. This can get tricky and if this is your problem let me know in the comments and I can give you some general advice as to some tricks you might try. I've gone from a minimal reflective solution to a full-on XML based solution. Were I to do it again, I might consider annotation-based.


Example of MVC:

Note, this is just an example, there are a MILLION ways to do MVC.

In your MAIN:

  • Instantiate MyView
  • Instantiate MyModel
  • Instantiate MyController(myView, myModel)
  • myView.setVisible(true)

in MyView

  • probably extends Frame
  • Most components are public final (public final Button b=new Button())
  • If public members make you nervous, use getters--same EXACT effect as public final members with a little extra syntax.
  • Remember that you can set final members in your constructor.
  • May have general methods such as reset(), but MyController may be a better place for this.

in MyController

  • saves references to myView and myModel
  • adds listeners to myView where necessary (see advice on listeners above)
  • configures myView based on state of myModel
  • when "done" button pressed, copies state from myView to myModel
  • notifies myModel that it's data has been updated and destroys itself.

in MyModel:

This would be a typical model class, it would contain your business logic (mostly not used as part of the GUI, that's more like GUI logic in MyController. The controller would tend to set values in your business logic then call some method like updated() to cause some business logic to take control. It should know nothing of a GUI--this is your "pure" business class.

Sometimes the GUI might call an update() or some other method to trigger some data change and then reload the GUI controls from the Model--this is a fairly good way to integrate your business logic with your GUI without your model knowing about the GUI...

Also, as I said above, I would put more work into MyController if I was working with property sheets just due to the sheer number of lines of boilerplate that you end up with if you aren't smart about it.

Note that View and Controller are nearly always paired. You can't just replace a Swing view with a web view and expect the controller to remain unmolested--but the model should not ever change for the view or controller.

Bill K
haha, i'm loving the drip feed :)so you suggest i instanciate a 'key handler' class in the begginng of my main page/class and then call handler methods inside this? or do i create an empty(ish) main class, and instanciate the three classes in here? my head, as you can see is going round in circles?where could i see a simple example you think?Thanks!
flyingcrab
brilliant. thanks!
flyingcrab
+3  A: 

You should take a look at the best practices for GWT applications first:

http://code.google.com/events/io/2009/sessions/GoogleWebToolkitBestPractices.html

One of the concepts they talk about is that of MVP (not MVC) to structure your application. There's a sample project on Google Code that you can look at to understand how to structure a GWT application in this way:

http://code.google.com/p/gwt-mvp/

Jon
I agree, MVC isn't the only model, in fact in general I don't love it--just one way to help you start to get organized. MVP is another--and everyone who implements either has a different way to view it. Really the trick is to just separate functionality to provide clean, minimal interfaces. Whatever methodology best helps you with that can be a personal choice. +1, good reference.
Bill K