Google Web Toolkit (GWT) MVP Example (1)

本文介绍如何使用 Google Web Toolkit (GWT) 的 MVP 模式重构默认启动应用,通过引入最佳实践如命令模式、事件总线和依赖注入等,使应用更易于扩展和测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转自:http://blog.hivedevelopment.co.uk/2009/08/google-web-toolkit-gwt-mvp-example.html

 

 

Here at Hive Development, I'm currently working on the GWT based UI for a new website/RIA monitoring service called SiteAlright. I recommend you head over and check it out. As you might expect we try to follow best practises when developing our apps and there's been quite a lot of talk recently on GWT Google Groups regarding the use of elements from the recent talk by Ray Ryan at Google I/O 2009 entitled Google Web Toolkit Architecture: Best Practices For Architecting Your GWTApp.

This excellent talk laid out several best practise approaches for architecting your GWT application based on the team's experiences while developing the new AdWords interface. Some of the key recommendations were:

The aim of this tutorial is to demonstrate these GWT best practises by applying them the default starter application generated by the Google Plugin for Eclipse.

There are many benefits to these patterns and I'll leave it to the links above to go into these in detail. However, using these patterns the bottom line is that your GWT application will be:

  • Easier to extend - well defined structure makes the addition of new business logic much easier;
  • Easier to test - the decoupling makes quick unit testing of commands, events and presenters not only possible but easy;
  • GWT's History mechanism is baked into your application from the start - this is crucial for a GWT application and is a pain to retro-fit;

MVP Web Application Starter Project

For this tutorial I'm using Eclipse with the Google Plugin for Eclipse. With the Google plugin when you create a GWT project, it also creates a useful starter application with some widgets and server components. This tutorial will work with this starter application and will apply the above patterns.

Although this example has all the source code and references to required resources, you may download the complete GreetMvp project for Eclipse 3.5 from here.

As well as the Google plugin, you'll also need the following GWT libraries:

GWT-PresenterAn implementation of the MVP pattern;
GWT-DispatchAn implementation of the command pattern;
Google GinDependency Injection based on Google's Guice;
GWT-LogA log4j-style logger for GWT.

NOTE: currently GIN needs to be built from source using SVN/Ant.

You'll also need the following libraries at the server:

log4jA logging framework;
Google Guice 2.0A dependency injection framework for Java.

After introducing these best practise patterns, the structure starter application will be transformed. At this point, it's probably worth noting that:

  • You'll no longer make RPC service calls directly - these are wrapped up in the command pattern and are handled by the gwt-dispatch library;
  • The main page and the server response popup will be separated into respective view/presenters;
  • The Event Bus pattern is implemented using the GWT 1.6+ Handlers mechanism.

Unsurprisingly, the code size will jump from the initial starter app, but what you'll end up with offers much more and will hopefully serve as the starting point for a real application based on these best practises.

Let's begin.

Fire up Eclipse and generate your starter application, I've called it GreetMvp:

google plugin eclipse create project 1 google plugin eclipse create project 2

This will generate the following structure:

gwt default application structure screenshot

If you're not already familiar with the default application I suggest you take a look at the entry point class which in my case is co.uk.hivedevelopment.greet.client.GreetMvp.java. You'll see all the view code, custom handler logic and server calls all lumped into one method. Fire up the application and you see the following:

gwt default application screenshot

The first thing we'll do is add the required libraries to the project - at this point you should have downloaded the listed dependencies and built Google Gin.

Create a folder called lib in your project at the top level of your project for client-only libraries and add the following:

gin.jar 

For server and client dependencies, add the remaining jars into the web application lib folder located at war/WEB-INF/lib:

aopalliance.jar(from Google Gin)
guice-2.0.jar(from Google Gin. IMPORTANT - use the version supplied with Gin and not Guice)
guice-servlet-2.0.jar(from Google Guice)
gwt-dispatch-1.0.0-SNAPSHOT.jar(from gwt-dispatch)
gwt-log-2.6.2.jar(from gwt-log)
gwt-presenter-1.0.0-SNAPSHOT.jar(from gwt-presenter)
log4j.jar(from log4j)

Add all of the above jars to the project's build path. You should have something that looks similar to this:

gwt default application structure screenshot

Edit the GWT module definition file co.uk.hivedevelopment.greet/GreetMvp.gwt.xml:

view source
<object id="highlighter_777931_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print ?
01. <?xml version="1.0" encoding="UTF-8"?>
02. <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
03. <module rename-to='greetmvp'>
04. <!-- Inherit the core Web Toolkit stuff.                        -->
05.  <inherits name="com.google.gwt.user.User" />
06.  <inherits name="com.google.gwt.inject.Inject" />
07.  <inherits name='net.customware.gwt.dispatch.Dispatch' />
08.  <inherits name='net.customware.gwt.presenter.Presenter' />
09. <!-- Inherit the default GWT style sheet.  You can change       -->
10. <!-- the theme of your GWT application by uncommenting          -->
11. <!-- any one of the following lines.                            -->
12.  <inherits name='com.google.gwt.user.theme.standard.Standard' />
13. <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
14. <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->
15. <!-- Specify the app entry point class.                         -->
16.  <entry-point class='co.uk.hivedevelopment.greet.client.GreetMvp' />
17. <!-- Add gwt-log support, default level `OFF` - check for
18.      extended property 'log_level' to see if this is overridden -->
19.  <inherits name="com.allen_sauer.gwt.log.gwt-log-OFF" />
20. <!-- Also compile Logger at `INFO` level -->
21.  <extend-property name="log_level" values="INFO" />
22.  <set-property name="log_level" value="INFO" />
23. <!-- Turn off the floating logger - output will be shown in the
24.   hosted mode console -->
25.  <set-property name="log_DivLogger" value="DISABLED" />
26.   
27.  <source path="shared" />
28.  <source path="client" />
29.</module>

Note the <source> tags. We have roughly 3 top level packages defined: client, shared and server. We do not want GWT accessing the server sub-packages so we have explicity told the GWT compiler what it can access.

Try to compile the project, it should compile cleanly.

Create View and Presenters



Now we'll split the app into the following components:
AppPresenter.javaRepresents the main application;
GreetingView.javaThe GUI components for the greeting example;
GreetingPresenterThe business logic for the greeting example;
GreetingResponseViewThe GUI for the reponse popup;
GreetingResponsePresenterThe business logic for the response popup.
AppPresenter.java
view source
<object id="highlighter_644217_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print ?
01. package co.uk.hivedevelopment.greet.client.mvp;
02. import net.customware.gwt.dispatch.client.DispatchAsync;
03. import com.google.gwt.user.client.ui.HasWidgets;
04. import com.google.inject.Inject;
05. public class AppPresenter {
06.  private HasWidgets container;
07.  private GreetingPresenter greetingPresenter;
08. @Inject
09.  public AppPresenter(final DispatchAsync dispatcher,
10.        final GreetingPresenter greetingPresenter) {
11.  this.greetingPresenter = greetingPresenter; 
12. }
13.   
14.  private void showMain() {
15.  container.clear();
16.  container.add(greetingPresenter.getDisplay().asWidget());
17. }
18.    
19.  public void go(final HasWidgets container) {
20.  this.container = container;
21.    
22.  showMain();
23. }
24.}
GreetingView.java
view source
<object id="highlighter_305023_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print ?
01. package co.uk.hivedevelopment.greet.client.mvp;
02. import net.customware.gwt.presenter.client.widget.WidgetDisplay;
03. import com.google.gwt.event.dom.client.HasClickHandlers;
04. import com.google.gwt.user.client.ui.Button;
05. import com.google.gwt.user.client.ui.Composite;
06. import com.google.gwt.user.client.ui.FlowPanel;
07. import com.google.gwt.user.client.ui.HasValue;
08. import com.google.gwt.user.client.ui.RootPanel;
09. import com.google.gwt.user.client.ui.TextBox;
10. import com.google.gwt.user.client.ui.Widget;
11. public class GreetingView extends Composite implements GreetingPresenter.Display {
12.  private final TextBox name;
13.  private final Button sendButton;
14.  public GreetingView() {
15.   final FlowPanel panel = new FlowPanel();
16.  initWidget(panel);
17.   name = new TextBox();
18.  panel.add(name);
19.   sendButton = new Button("Go");
20.  panel.add(sendButton);
21.    
22.  // Add the nameField and sendButton to the RootPanel
23.  // Use RootPanel.get() to get the entire body element
24.  RootPanel.get("nameFieldContainer").add(name);
25.  RootPanel.get("sendButtonContainer").add(sendButton);
26.    
27.  reset();
28. }
29.  public HasValue getName() {
30.   return name;
31. }
32.  public HasClickHandlers getSend() {
33.   return sendButton;
34. }
35.   
36.  public void reset() {
37.  // Focus the cursor on the name field when the app loads
38.  name.setFocus(true);
39.  name.selectAll();
40. }
41. /**
42.  * Returns this widget as the {@link WidgetDisplay#asWidget()} value.
43.  */
44.  public Widget asWidget() {
45.   return this;
46. }
47. @Override
48.  public void startProcessing() {
49. }
50. @Override
51.  public void stopProcessing() {
52. }
53.}
内容概要:本文针对火电厂参与直购交易挤占风电上网空间的问题,提出了一种风火打捆参与大用户直购交易的新模式。通过分析可再生能源配额机制下的双边博弈关系,建立了基于动态非合作博弈理论的博弈模型,以直购电价和直购电量为决策变量,实现双方收益均衡最大化。论文论证了纳什均衡的存在性,并提出了基于纳什谈判法的风-火利益分配方法。算例结果表明,该模式能够增加各方收益、促进风电消纳并提高电网灵活性。文中详细介绍了模型构建、成本计算和博弈均衡的实现过程,并通过Python代码复现了模型,包括参数定义、收益函数、纳什均衡求解、利益分配及可视化分析等功能。 适合人群:电力系统研究人员、能源政策制定者、从事电力市场交易的工程师和分析师。 使用场景及目标:①帮助理解风火打捆参与大用户直购交易的博弈机制;②为电力市场设计提供理论依据和技术支持;③评估不同政策(如可再生能源配额)对电力市场的影响;④通过代码实现和可视化工具辅助教学和研究。 其他说明:该研究不仅提供了理论分析,还通过详细的代码实现和算例验证了模型的有效性,为实际应用提供了参考。此外,论文还探讨了不同场景下的敏感性分析,如证书价格、风电比例等对市场结果的影响,进一步丰富了研究内容。
资源下载链接为: https://pan.quark.cn/s/d37d4dbee12c A:计算机视觉,作为人工智能领域的关键分支,致力于赋予计算机系统 “看懂” 世界的能力,从图像、视频等视觉数据中提取有用信息并据此决策。 其发展历程颇为漫长。早期图像处理技术为其奠基,后续逐步探索三维信息提取,与人工智能结合,又经历数学理论深化、机器学习兴起,直至当下深度学习引领浪潮。如今,图像生成和合成技术不断发展,让计算机视觉更深入人们的日常生活。 计算机视觉综合了图像处理、机器学习、模式识别和深度学习等技术。深度学习兴起后,卷积神经网络成为核心工具,能自动提炼复杂图像特征。它的工作流程,首先是图像获取,用相机等设备捕获视觉信息并数字化;接着进行预处理,通过滤波、去噪等操作提升图像质量;然后进入关键的特征提取和描述环节,提炼图像关键信息;之后利用这些信息训练模型,学习视觉模式和规律;最终用于模式识别、分类、对象检测等实际应用。 在实际应用中,计算机视觉用途极为广泛。在安防领域,能进行人脸识别、目标跟踪,保障公共安全;在自动驾驶领域,帮助车辆识别道路、行人、交通标志,实现安全行驶;在医疗领域,辅助医生分析医学影像,进行疾病诊断;在工业领域,用于产品质量检测、机器人操作引导等。 不过,计算机视觉发展也面临挑战。比如图像生成技术带来深度伪造风险,虚假图像和视频可能误导大众、扰乱秩序。为此,各界积极研究检测技术,以应对这一问题。随着技术持续进步,计算机视觉有望在更多领域发挥更大作用,进一步改变人们的生活和工作方式 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值