我想使用相同的代码在客户端和服务器端对对象进行排序和操作。
但我遇到了一个问题,因为在客户端中我们需要一个代表服务器类的代理接口(interface)。
有没有办法在两者中使用相同的接口(interface)?我知道 RF 有一种机制可以在通过网络发送时将 bean 属性从服务器实例复制到客户端实例。
最佳答案
使用相同 API 的一种方法是使用代理扩展和域对象实现的接口(interface)。
// common interfaces
interface Foo { … }
interface Bar<T extends Foo> {
int getX();
void setX(int x);
// setters need to use generics
List<T> getFoos();
void setFoos(List<T> foos);
// with only a getter, things get easier:
Bar getParent();
}
// domain objects
class RealFoo implements Foo { … }
class RealBar implements Bar<RealFoo> {
int x;
List<RealFoo> foos;
RealBar parent;
@Override
public RealBar getParent() { return parent; }
// other getters and setters
}
// proxy interfaces
@ProxyFor(RealFoo.class)
interface FooProxy extends Foo { … }
@ProxyFor(RealBar.class)
interface BarProxy extends Bar<FooProxy> {
@Override
BarProxy getParent();
// other getters and setters
}
然后您可以在客户端和服务器端使用 Comparator<Foo>
或 Comparator<Bar>
。
我通常只实现 traits(aspects,facets,按你喜欢的方式调用它们)虽然( HasId
, HasLabel
, HasPosition
等),而不是完整的域对象的 API。然后我可以使用 HasId
获取任何对象的键以将它们放入 map 或比较是否相等,HasLabel
用于显示(客户端自定义 Cell
,服务器端发送到客户端的错误消息,等), HasPosition
用于排序等
关于java - GWT 射频 : How to share the same code in client and server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15849602/