开发者

java / gwt UI coding - clean code

开发者 https://www.devze.com 2022-12-20 20:47 出处:网络
i\'ve started on some basic java coding with gwt, and im getting a bit concerned about the heft of my main class.

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开发者_如何学Python own tuts are pretty basic, and just kitchen-sink every thing into a single file)

thanks!


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号