Search code examples
apifactory-patternjung

Jung Graph Package and Factory Patterns


I am trying to initialize each of the following where the Graph Object comes from the JUNG graph package (Factory comes from org.apache.commons.collections15):

        Factory<Graph<Integer, String>> graphFactory;
        Factory<Integer> vertexFactory;
        Factory<String> edgeFactory;

I need these three objects in the constructor of the BarabasiAlbertGenerator (click for JavaDoc) Object.

In my reading of the Factory Pattern, I see that it is used to pass off the instantiation of objects to subclasses. So, what I know is:

  1. There must be some class in the Jung Package that implements the Factory interface that can then instantiate the object I declare 'graphFactory'. Similarly for vertexFactory and edgeFactory

The question is (is what I am assuming correct as well) how do I instantiate these objects and which set of possible types could/would I use for instantiating each object?


Solution

  • For the graphFactory, you can use the getFactory() static method on the class of graph that you want.

    You will need to write instances of vertexFactory and edgeFactory.
    You can use any type for vertex/edge, that's why they're generic (and that's why you have to specify how they're created). :)
    Check out the sample code for examples.