Guice 如何根据 String id 提供不同的子类实例

发布于 2024-12-04 09:30:40 字数 1840 浏览 0 评论 0 原文

我有一个工厂类用例,我想用 Guice 实现,但不知道如何实现。 我有一个名为 Action 的抽象类,它代表用户可以在我的应用程序上执行的不同类型的操作。 每个Action都是Action类的子类,并且每个Action也都有一个String类型的标识。 因为操作是重对象,所以我不想立即将其全部实例化,因此我提供了一个工厂来根据客户端要求的 ID 来实例化每个操作。

工厂接口如下所示:

public interface ActionFactory {

    Action getActionByID(String id);

}

我们对此工厂的实现使用 HashMap 来维护 String 实例和所谓的 ActionInstantiator 之间的关系,该 ActionInstantiator 将提供具体的 Action 实例。 其实现如下所示:

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());
        .....
        .....
    }
}

因此,某些可以使用此工厂并希望 ActionAAA 实例类的客户端可以这样调用它:

Action action=ActionFactoryImpl.instance().getActionByID(actionId);

其中 actionId 是在运行时从数据库获取的。

我发现某种注释注入可以做类似的事情,但就我而言,我认为这是行不通的,因为我只知道用户在运行时需要的实例,所以我无法在代码上注释。

我是 Guice 的新手,所以也许这是我在文档中找不到的非常常见的东西,如果是这样的话,我很抱歉。 任何帮助将不胜感激。 问候 丹尼尔

I have a Factory Class use case I want to implement with Guice, but not sure how.
I have an Abstract Class named Action which represent different kind of actions the user could perform on my app.
Each of the Actions are subclasses of Action class, and each of them also have an identification of String type.
Because Actions are heavy objects I don't want to have it all instanciated at once, so I provides a Factory to instanciate each of them depending on the ID the client ask for.

The Factory Interface looks like:

public interface ActionFactory {

    Action getActionByID(String id);

}

Our implementation of this Factory uses a HashMap to maintain the relationship between the String instance and a so called ActionInstantiator that will provides the concrete Action instance.
Implementation of this looks like:

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());
        .....
        .....
    }
}

So some client that could use this factory and wants ActionAAA instance class calls it like this:

Action action=ActionFactoryImpl.instance().getActionByID(actionId);

Where actionId was obtained at runtime from database.

I found that some kind of annotation injection could do something similar, but in my case I think that that wouldn't work, because I only know the instance that the user will requieres at runtime, so I couldn't annotated on the code.

I'm new to Guice so maybe this is something very common I couldn't found in the docs, I appologies if that is the case.
Any help will be appreciated.
Regards
Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-12-11 09:30:40

您想要使用 Multibindings 扩展,特别是 MapBinder。您可能希望您的 ActionInstantiator 类型实现 Provider。然后你可以这样做:

MapBinder<String, Action> mapbinder
     = MapBinder.newMapBinder(binder(), String.class, Action.class);
mapbinder.addBinding("actionAAA", ActionAAAInstantiator.class);
// ...

然后你可以在你想要的地方注入一个 Map> 。您还可以将内容注入到您的 ActionInstantiator 中。

You want to use the Multibindings extension, specifically MapBinder. You probably want your ActionInstantiator type to implement Provider<Action>. Then you can do:

MapBinder<String, Action> mapbinder
     = MapBinder.newMapBinder(binder(), String.class, Action.class);
mapbinder.addBinding("actionAAA", ActionAAAInstantiator.class);
// ...

Then you can inject a Map<String, Provider<Action>> where you want. You'll also be able to inject things in to your ActionInstantiators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文