有没有人见过/尝试编写使用Guice风格配置系统的服务定位器模式?

目前我有一个GWT项目(碰巧使用GWT-RPC)使用命令模式,其中我的RPC servlet看起来像这样......

public interface TransactionService extends RemoteService {

    <T extends Response> T execute(Action<T> action);
}

在我目前执行的execute方法中,我这样做......

if(action instanceof SomeActionImpl){
  doSomeActionImpl((SomeActionImpl)action);
}else if(action instanceof SomeActionImpl2){
  doSomeActionImpl2((SomeActionImpl2)action);
}

我想做的是想办法摆脱巨大的if语句。我需要一些注册方法,将ActionImpl1的类委托给TransactioNService的另一个实现。

有什么想法吗?我想只是在HashMap中添加条目,其中键是Action的类,值是ServiceImpl的类。我有一个对ServiceImpl类的引用,我可以使用Guice来获取TransactionService的实例。

有帮助吗?

解决方案

查看gwt-dispatch中的net.customware.gwt.dispatch.server.DefaultActionHandlerRegistry类( http://code.google.com/p/gwt-dispatch/ );它完全符合你的建议。以下是存储处理程序的成员变量:

私人最终地图<!> lt; Class <!> lt;?扩展Action <!> lt;?<!> gt; <!> gt;,ActionHandler <!> lt;?,?<!> gt; <!> gt;处理程序;

如果要在服务器端执行处理程序,则使用gwt-dispatch服务器端组件;如果它是客户端的东西,那么考虑在DefaultActionHandlerRegistry上建模你的调度类。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top