使用Google Web Toolkit我编写了一个基于模型 - 视图 - 控制器概念的应用程序。现在我的ClientController
类有两个类型ClientModel
和ClientView
的对象,它们被声明为私有和非静态。对于异步调用的对象,我已经编写了一些可行的远程过程调用,但是从类型AsyncCallback<T>
附加匿名对象会让我遇到问题,因为我无法访问这两个ClientModel
和{{ 1}}对象没有在我的函数中编写一些讨厌的ClientView
变量,如下所示:
final
现在我正在使用package com.foo.bar;
/**
* Represents the main handler holding delegates and controlling the contents of
* the {@link ClientModel} and pushes notifications and message to both the
* {@link ClientModel} and the {@link ClientView}.
*/
public class ClientController {
/**
* Exposes asynchronous functions to call the server component via RPC.
*/
public static final MyServiceAsync mySvc = GWT.create(myService.class);
/**
* Represents the model associated with the {@link ClientController} object.
*/
private ClientModel theModel = null;
/**
* Represents the view associated with the {@link ClientController} object.
*/
private ClientView theView = null;
/**
* Creates a new {@link ClientController} object and instantiates both the
* {@link ClientModel} and the {@link ClientView}.
*/
public ClientController() {
this.theModel = new ClientModel();
this.theView = new ClientView(this.theModel);
}
/* some more code */
/**
* Tries to login the specified user and updates the {@link ClientView}
* object to either an error message or the main interface.
*
* @param user
* {@link User} object representing the user to login
*/
public void loginUser(final User user) {
///////////////////////////////////////////////
// THIS IS UGLY AND I DON'T KNOW HOW TO FIX THIS
///////////////////////////////////////////////
final ClientModel currentModel = this.theModel;
// Execute the login protocol
ClientController.mySvc.login(user, new AsyncCallback<Boolean>() {
/**
* The request was successfully executed. Returns a boolean value
* indicating whether the user was logged in.
*
* @param result
* true, if the user was logged in; otherwise, false.
*/
@Override
public void onSuccess(Boolean result) {
// The user was successfully logged in and we must both store
// him in the model and then update the view.
if (result) {
// TODO: Update the view to show the chat itself and save
// the current User to the ClientModel.
System.out.println("The User " + user.getUsername()
+ " is now logged in!");
// Anonymous object can not access the theModel and theView
// objects of ClientController directly ...
// drunk, fix later!
// UGLY FIX FOR NOW
currentModel.setCurrentUser(user);
} else {
// TODO: Unhide the error label of the login form and output
// some nice error message.
System.out.println("Login failed for User "
+ user.getUsername() + "!");
}
}
/**
* The request provoked an error.
*/
@Override
public void onFailure(Throwable up) {
try {
throw up; // Ha ha
} catch (Throwable e) {
// Who cares?
}
}
});
}
}
内部模型的最终指针,该指针有两次高亮显示。任何人都可以解释我是否有一个更好的解决方案,而不将loginUser
和theModel
移动到静态成员?在访问任一组件的每个函数中编写这样的“最终包装器”是很烦人的......
答案 0 :(得分:3)
当您创建非静态内部类的实例或匿名类的实例时,该实例具有对外部的实例的隐式绑定创建它的类。您可以使用OuterClassName.this.member
从内部类中访问外部类的成员。
在您的情况下:ClientController.this.theModel
答案 1 :(得分:0)
尝试并宣布您的成员是最终的,如下所示:
...
private final ClientModel theModel;
private final ClientView theView;
public ClientController() {
this.theModel = new ClientModel();
this.theView = new ClientView(this.theModel);
}