I may have understood. I have manage to implement a solution. I hope
it it with the good wauy.
So in my structure module,
I have this factory :
public interface ISImpleGraphFactory {
/**
* create a SimplrGraph.
* @return a graph
*/
ISimpleGraph createGraph();
/**
* create a SimpleNode.
* @param graph which will contained the node
*/
ISimpleNode createNode();
/**
* create a simple edge.
* @param source of the edge
* @param sink of the edge
* @return an edge
*/
ISimpleEdge createEdge(ISimpleNode source, ISimpleNode sink);
}
and this module :
public class SimpleGraphStructureModule extends AbstractModule {
@Override
protected void configure() {
bind(ISImpleGraphFactory.class).to(SimpleGraphFactoryImpl.class).in(Singleton.class);
}
}
in an other maven module (the demo modul) I have this provider :
public class DemoProvider implements Provider<ISimpleGraph> {
private ISimpleGraph demo;
private final Injector injector;
@Inject
private DemoProvider() {
this.injector = Guice.createInjector(new
SimpleGraphStructureModule());
}
private void builDemo() {
ISImpleGraphFactory factory =
this.injector.getInstance(ISImpleGraphFactory.class);
this.demo = factory.createGraph();
ISimpleNode node1 = factory.createNode();
ISimpleNode node2 = factory.createNode();
ISimpleNode node3 = factory.createNode();
ISimpleEdge edge1 = factory.createEdge(node1, node2);
this.demo.addNode(node1);
this.demo.addNode(node2);
this.demo.addNode(node3);
this.demo.addEdge(edge1);
// System.out.println("graph = " + demo);
}
public ISimpleGraph get() {
if (this.demo == null) {
this.builDemo();
}
return this.demo;
}
}
this module :
public class DemoModule extends AbstractModule {
@Override
protected void configure() {
bind(ISimpleGraph.class).toProvider(DemoProvider.class);
}
}
and to test it, I have this Main class :
public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new DemoModule());
ISimpleGraph demo = injector.getInstance(ISimpleGraph.class);
System.out.println(demo);
}
}
With this, I have the good result.
Do you think it is a good way to use google guice for this sample
example?
Jérémie
On 17 juil, 15:59, Maaartin-1 <[email protected]> wrote:
> On 10-07-16 16:00, Jerem's wrote:
>
>
>
>
>
> > Sorry for the long time to answer. I had not time to test it before.
> > I am not sure that your solution is good for me. I don"t want to
> > switch implementation. I want the many "services' use the same
> > structure.
>
> > I think I have finished to develop my structure: this is my module :
>
> > [CODE]
> > @Override
> > protected void configure() {
>
> > bind(ISImpleGraphFactory.class).to(SimpleGraphFactoryImpl.class).in(Singlet
> > on.class);
> > bind(ISimpleGraph.class).to(SimpleGraphImpl.class);
> > bind(ISimpleNode.class).to(SimpleNodeImpl.class);
> > bind(ISimpleEdge.class).to(SimpleEdgeImpl.class);
> > }
> > [/CODE]
>
> > I use a factory only to create edge (because edge need instance of 2
> > nodes).
>
> For me this makes no sense. Do you really want to ask Guice for each
> single Node? Imagine each Node is immutable (as it should be if
> possible) and contains its name. How do you plan to make Guice know
> what's the name of the new Node?
>
> I'm quite sure you're going to make your life harder this way. Simply
> asking Guice for NodeFactory, EdgeFactory, and GraphFactory would IMHO
> do all you need.
>
> > now I would like to create a service. A simple service called
> > DemoService. this service create juste a simple graph.
> > this service is implemented in an other maven project.
> > How can I make work together the module of graphStructure project and
> > the module of the demoProject ?
> > I would like to limits the dependances between these two project.
>
> Don't let the dependent project know the implementations, let it see the
> interfaces only (extract them to a third project).
>
> > And I have read the we should not use too much this line :
>
> > Injector injector = Guice.createInjector(new
> > SimpleGraphStructureModule());
>
> > What is the guice good way to do that?
>
> Ideally, you should create only one injector and ask it for only one
> object, which provides the root service you need. However, the world is
> not ideal, so you may need child injectors, passing injectors around, or
> whatever. That said, I'm not sure if I understood your question.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.