java/gwt UI编码-干净的代码 [英] java / gwt UI coding - clean code

查看:47
本文介绍了java/gwt UI编码-干净的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用gwt进行一些基本的Java编码,而我有点担心主类的繁重.

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

例如-如何分隔密钥处理程序,因为它们触发了UI的许多更改,我该如何将其移到单独的.class文件中,并且仍然能够访问主类中的所有各种小部件,无需将所有内容都传递给处理程序(即,我在click事件之后处理的所有小部件).

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).

我已经搜索过,但是没有遇到任何特别好的例子-知道我可以阅读任何易于阅读的代码库以了解应该如何做吗?(gwt自己的tut很基本,只是把所有东西都放在一个文件中)

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)

谢谢!

推荐答案

我不愿意说些难以想象的东西,但是MVC可以工作-这不是最终的方法,但是它可以使您井井有条.

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

在搜索一个半相关主题时,我遇到了

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

就GWT而言,这意味着您应该考虑将GUI组件放在一个类中,将所有事件处理放在第二个类中,然后将您的对象模型对象与其他两个对象分开.

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.

一种实现此目的的方法是将GUI上的大多数或所有控件公开.这听起来有些la脚,但是它们的用法封装在控制器内,所以这并不是您具有不可控的访问权限-实际上,与所有成员都是私有的相比,访问权限的定义更清晰/定义更好,但您的视图代码已与控制器组合在一起.

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.

特定技巧:

让侦听器成为自己的类.您经常可以重用它们-换句话说,避免使用匿名内部类.有时我会创建一个侦听器类,并为每个按钮/控件实例化一个新实例,这些按钮/控件在按下时需要具有相似的效果.如果我需要它对给定的按钮采取稍有不同的操作,则将某些内容传递给特殊"处理程序的构造函数,以使他们知道某些不同的操作.如果需要,您还可以创建不同的处理程序子类-我只是说不要忘记事件处理程序是类,可以使用继承,并且在需要时可以使用所有内容.

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.

一个非常古老的GUI技巧我很久以前就学过,请不要让各种微型处理程序以不同的方式修改GUI,而是让所有活动"按钮和控件在GUI中设置一个状态,然后调用一个将该状态应用于GUI上所有控件的单个方法.当您超越普通的GUI时,这可能会节省生命.如果不清楚,请发表评论,并为您提供示例.

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.

属性表:

GUI有一种特殊情况-属性表样式GUI.我已经做了很多这些,它们使人为地狱而烦恼.它们往往具有数十个或数百个控件,每个GUI控件往往都绑定到模型中的特定字段,并且只有几百行复制和粘贴样板代码连接它们,每个组都复制并粘贴了几行项目已更改-每个控件至少需要3行代码(创建控件,复制值输入和复制值输出).

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).

我总是使用智能"控制器来编写这些代码,该控制器可以智能地将控件绑定到某些数据,而无需任何唯一代码.这可能会很棘手,如果这是您的问题,请在评论中让我知道,我可以为您提供一些可能会尝试的技巧的一般建议.我已经从最小的反射解决方案变成了基于XML的完整解决方案.如果我要再做一次,我可能会考虑基于注释.

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.

MVC示例:

请注意,这只是一个示例,有数百万种方法可以进行MVC.

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

在您的主要帐户中:

  • 实例化MyView
  • 实例化MyModel
  • 实例化MyController(myView,myModel)
  • myView.setVisible(true)

在MyView中

  • 可能扩展框架
  • 大多数组件都是public final(public final Button b = new Button())
  • 如果公共成员使您感到紧张,请使用吸气剂-与公共最终成员具有完全相同的效果,只是需要一些额外的语法.
  • 请记住,您可以在构造函数中设置最终成员.
  • 可能具有诸如reset()之类的常规方法,但是MyController可能是一个更好的选择.

在MyController中

in MyController

  • 保存对myView和myModel的引用
  • 在必要时将侦听器添加到myView中(请参阅上面有关侦听器的建议)
  • 根据myModel的状态配置myView
  • 按下完成"按钮时,会将状态从myView复制到myModel
  • 通知myModel数据已更新并自行销毁.

在MyModel中:

这将是一个典型的模型类,它将包含您的业务逻辑(通常不用作GUI的一部分,这更像MyController中的GUI逻辑.控制器倾向于在您的业务逻辑中设置值,然后调用某种方法就像update()引起一些业务逻辑来控制一样.它应该对GUI完全一无所知-这是您的纯"业务类.

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.

有时,GUI可能会调用update()或其他方法来触发一些数据更改,然后从模型中重新加载GUI控件-这是一种在模型不知道的情况下将业务逻辑与GUI集成的相当不错的方法关于GUI ...

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...

而且,正如我在上面说的那样,如果我使用属性表,我会在MyController中投入更多的工作,这是因为如果您不了解它,最终可能会遇到很多样板行.

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.

请注意,View和Controller几乎总是配对.您不能只将Swing视图替换为Web视图并期望控制器保持不受干扰-但是视图或控制器的模型永远都不应更改.

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.

这篇关于java/gwt UI编码-干净的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆