Another way to manage geometry factories is through the use of a container. This requires you to register the specific factory implementations you want to use.
Below we create a PicoContainer with all the required factories registered; we will then use the container to get the factories we will need and the container will figure out all the dependencies for us.
The call to create the PicoContainer will have to call into the org.picocontainer PicoContainer package.
DefaultPicoContainer cont = new DefaultPicoContainer(); // Teach Container about Factory Implementations we want to use cont.registerComponentImplementation(PositionFactoryImpl.class); cont.registerComponentImplementation(AggregateFactoryImpl.class); cont.registerComponentImplementation(ComplexFactoryImpl.class); cont.registerComponentImplementation(GeometryFactoryImpl.class); cont.registerComponentImplementation(PrimitiveFactoryImpl.class); // Teach Container about other dependencies needed cont.registerComponentInstance( DefaultGeographicCRS.WGS84); cont.registerComponentInstance( new PrecisionModel());
We made a PicoContainer, registered the factories we were going to use and then added two qualifiers. We use the static CoordinateReferencingSystem (CRS) object WGS84 to tell the container we want to use the CRS of the GPS system. We then tell the container we want to use the default precision model.
The general idea is to create a different container for each CRS and/or precision model you want to use. We could therefore put the above code in a method and pass as parameters the CRS and PrecisionModel.
Obtain the Factory interfaces
Using the container we just made we can get some factories but we get the interface back rather than the implementation.
PositionFactory posF = cont.getComponentInstanceOfType( PositionFactory.class ); PrimitiveFactory primF = cont.getComponentInstanceOfType( PrimitiveFactory.class ); GeometryFactory geomF = cont.getComponentInstanceOfType( GeometryFactory.class );
Here we simply asked the container to give us the factories of the different types we would need.