Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI提供程序_Java_Guice - Fatal编程技术网

Java GUI提供程序

Java GUI提供程序,java,guice,Java,Guice,我有一个小问题,我想不出办法来救我的命 基本上,我需要随时使用guice动态注册类,然后遍历它们 假设这是我的类来注册策略,但是这些策略可以通过运行的应用程序随时添加 // Strategy registration may happen anytime, this is just an example strategyManager.register(ExampleStrategy1.class); strategyManager.register(ExampleStrategy2.class

我有一个小问题,我想不出办法来救我的命

基本上,我需要随时使用guice动态注册类,然后遍历它们

假设这是我的类来注册策略,但是这些策略可以通过运行的应用程序随时添加

// Strategy registration may happen anytime, this is just an example
strategyManager.register(ExampleStrategy1.class);
strategyManager.register(ExampleStrategy2.class);
战略执行类

public class StrategyImpl implements Strategy {

    @Override
    public void register(Class<? extends StrategyDispatcher> strat) {
        //Add this class into provider or create an instance for it and add it into guice but how?
    }

    @Override
    public void dispatchStrategy() {
        //Find all strategies and execute them
    }

}
我的provider类总是获得相同的实例

public class StrategyProvider implements Provider<StrategyDispatcher> {

    public LogManager get() {
        return new StrategyDispatcherImpl();
    }

}
我添加的策略扩展了StrategyDispatcherImpl类,这样我就可以使用它们了


我需要向同一个实例添加多个绑定,但需要动态地完成,并且不使用configure中的bind方法,而是使用另一种方法,然后才能找到所有这些策略并执行它们

如果您真的需要它在应用程序生命周期的任何时候发生,那么Guice,那么我认为您将需要某种Guice感知工厂。即

public class TestStuff {
    @Test
    public void testDynamicCreation() {
        Injector injector = Guice.createInjector();
        StrategyManager manager  = injector.getInstance(StrategyManager.class);
        Hello hello = injector.getInstance(Hello.class);
        manager.doStuff();
        assertThat(hello.helloCalled, is(false));

        manager.register(Hello.class); // DYNAMIC!!
        manager.doStuff();
        assertThat(hello.helloCalled, is(true));
    }
}

interface Strategy {
    void doStuff();
}

@Singleton
class Hello implements Strategy {
    boolean helloCalled = false;
    public void doStuff() {
        helloCalled = true;
    }
}


class StrategyManager {
    private final Collection<Strategy> strategies = new ArrayList<>();
    private final StrategyFactory factory;

    @Inject
    StrategyManager(StrategyFactory factory) {
        this.factory = factory;
    }

    public void register(Class<? extends Strategy> strat) {
        strategies.add(factory.create(strat));
    }

    public void doStuff() {
        for (Strategy s : strategies) {
            s.doStuff();
        }
    }
}

class StrategyFactory {
    private final Injector injector;

    @Inject
    StrategyFactory(Injector injector) {
        this.injector = injector;
    }

    public Strategy create(Class<? extends Strategy> clazz) {
        return injector.getInstance(clazz);
    }
}

如果在初始化阶段之后它不是动态的,那么您就是在我想。

您好,您能告诉我您注册类的方法吗?manager.registerHello.class;//动态多斯塔夫经理;manager.registerHello.class;是如何注册类的。你可以用你想注册的类来调用它。你可能想尝试的另一件事是hk2,它专门设计为动态的,允许在任何时候添加和删除服务,而不仅仅是在初始化时
public class TestStuff {
    @Test
    public void testDynamicCreation() {
        Injector injector = Guice.createInjector();
        StrategyManager manager  = injector.getInstance(StrategyManager.class);
        Hello hello = injector.getInstance(Hello.class);
        manager.doStuff();
        assertThat(hello.helloCalled, is(false));

        manager.register(Hello.class); // DYNAMIC!!
        manager.doStuff();
        assertThat(hello.helloCalled, is(true));
    }
}

interface Strategy {
    void doStuff();
}

@Singleton
class Hello implements Strategy {
    boolean helloCalled = false;
    public void doStuff() {
        helloCalled = true;
    }
}


class StrategyManager {
    private final Collection<Strategy> strategies = new ArrayList<>();
    private final StrategyFactory factory;

    @Inject
    StrategyManager(StrategyFactory factory) {
        this.factory = factory;
    }

    public void register(Class<? extends Strategy> strat) {
        strategies.add(factory.create(strat));
    }

    public void doStuff() {
        for (Strategy s : strategies) {
            s.doStuff();
        }
    }
}

class StrategyFactory {
    private final Injector injector;

    @Inject
    StrategyFactory(Injector injector) {
        this.injector = injector;
    }

    public Strategy create(Class<? extends Strategy> clazz) {
        return injector.getInstance(clazz);
    }
}