Java Guice如何根据字符串id提供不同的子类实例
我有一个工厂类用例,我想用Guice实现,但不确定如何实现。 我有一个名为Action的抽象类,它表示用户可以在我的应用程序上执行的不同类型的操作。 每个操作都是Action类的子类,并且每个操作都有一个字符串类型的标识。 因为动作是重的对象,我不想一次实例化所有动作,所以我提供了一个工厂来实例化它们,具体取决于客户端要求的ID 工厂界面如下所示:Java Guice如何根据字符串id提供不同的子类实例,java,guice,Java,Guice,我有一个工厂类用例,我想用Guice实现,但不确定如何实现。 我有一个名为Action的抽象类,它表示用户可以在我的应用程序上执行的不同类型的操作。 每个操作都是Action类的子类,并且每个操作都有一个字符串类型的标识。 因为动作是重的对象,我不想一次实例化所有动作,所以我提供了一个工厂来实例化它们,具体取决于客户端要求的ID 工厂界面如下所示: public interface ActionFactory { Action getActionByID(String id); }
public interface ActionFactory {
Action getActionByID(String id);
}
public class ActionFactoryImpl implements ActionFactory {
private HashMap<String, ActionInstantiator> actions;
private static ActionFactoryImpl instance;
protected ActionFactoryImpl(){
this.actions=new HashMap<String, ActionInstantiator>();
this.buildActionRelationships();
}
public static ActionFactoryImpl instance(){
if(instance==null)
instance=new ActionFactoryImpl();
return instance;
}
public Action getActionByID(String id){
ActionInstantiator ai = this.actions.get(id);
if (ai == null) {
String errMessage="Error. No action with the given ID:"+id;
MessageBox.alert("Error", errMessage, null);
throw new RuntimeException(errMessage);
}
return ai.getAction();
}
protected void buildActionRelationships(){
this.actions.put("actionAAA",new ActionAAAInstantiator());
this.actions.put("actionBBB",new ActionBBBInstantiator());
.....
.....
}
}
这个工厂的实现使用一个HashMap来维护字符串实例和一个所谓的ActionInstantiator之间的关系,ActionInstantiator将提供具体的Action实例。
这项计划的实施情况如下:
public interface ActionFactory {
Action getActionByID(String id);
}
public class ActionFactoryImpl implements ActionFactory {
private HashMap<String, ActionInstantiator> actions;
private static ActionFactoryImpl instance;
protected ActionFactoryImpl(){
this.actions=new HashMap<String, ActionInstantiator>();
this.buildActionRelationships();
}
public static ActionFactoryImpl instance(){
if(instance==null)
instance=new ActionFactoryImpl();
return instance;
}
public Action getActionByID(String id){
ActionInstantiator ai = this.actions.get(id);
if (ai == null) {
String errMessage="Error. No action with the given ID:"+id;
MessageBox.alert("Error", errMessage, null);
throw new RuntimeException(errMessage);
}
return ai.getAction();
}
protected void buildActionRelationships(){
this.actions.put("actionAAA",new ActionAAAInstantiator());
this.actions.put("actionBBB",new ActionBBBInstantiator());
.....
.....
}
}
其中,actionId是在运行时从数据库中获取的
我发现某种注释注入可以做类似的事情,但在我的例子中,我认为那是行不通的,因为我只知道用户在运行时需要的实例,所以我无法对代码进行注释
我是Guice的新手,所以这可能是我在文档中找不到的非常常见的东西,如果是这样的话,我很抱歉。
任何帮助都将不胜感激。
当做
Daniel您特别想使用Multibindings扩展。您可能希望您的
操作实例化器
类型实现提供程序
。然后你可以做:
MapBinder<String, Action> mapbinder
= MapBinder.newMapBinder(binder(), String.class, Action.class);
mapbinder.addBinding("actionAAA", ActionAAAInstantiator.class);
// ...
MapBinder MapBinder
=MapBinder.newMapBinder(binder(),String.class,Action.class);
addBinding(“actionAAA”,actionAAAAInstantiator.class);
// ...
然后你可以在你想要的地方注入一个
Map
。您还可以将内容注入到您的操作实例化器中。感谢您的回答Colin,我认为这可能是使用Guice 3.0为我的服务器提供的解决方案。但是我需要一个可以在GWT客户端用Gin 1.5实现的解决方案。@Daniel:bind(ActionFactory.class)。to(ActionFactoryImpl.class)
?