Class CompositeFactory

java.lang.Object
org.pipservices3.components.build.CompositeFactory
All Implemented Interfaces:
IFactory

public class CompositeFactory
extends Object
implements IFactory
Aggregates multiple factories into a single factory component. When a new component is requested, it iterates through factories to locate the one able to create the requested component.

This component is used to conveniently keep all supported factories in a single place.

### Example ###

 
 CompositeFactory factory = new CompositeFactory();
 factory.add(new DefaultLoggerFactory());
 factory.add(new DefaultCountersFactory());
 
 Descriptor loggerLocator = new Descriptor("*", "logger", "*", "*", "1.0");
 factory.canCreate(loggerLocator); 		// Result: Descriptor("pip-service", "logger", "null", "default", "1.0")
 factory.create(loggerLocator); 			// Result: created NullLogger
 
 
  • Constructor Details

    • CompositeFactory

      public CompositeFactory()
      Creates a new instance of the factory.
    • CompositeFactory

      public CompositeFactory​(IFactory... factories)
      Creates a new instance of the factory.
      Parameters:
      factories - a list of factories to embed into this factory.
  • Method Details

    • add

      public void add​(IFactory factory)
      Adds a factory into the list of embedded factories.
      Parameters:
      factory - a factory to be added.
    • remove

      public void remove​(IFactory factory)
      Removes a factory from the list of embedded factories.
      Parameters:
      factory - the factory to remove.
    • canCreate

      public Object canCreate​(Object locator)
      Checks if this factory is able to create component by given locator. This method searches for all registered components and returns a locator for component it is able to create that matches the given locator. If the factory is not able to create a requested component is returns null.
      Specified by:
      canCreate in interface IFactory
      Parameters:
      locator - a locator to identify component to be created.
      Returns:
      a locator for a component that the factory is able to create.
    • create

      public Object create​(Object locator) throws CreateException
      Creates a component identified by given locator.
      Specified by:
      create in interface IFactory
      Parameters:
      locator - a locator to identify component to be created.
      Returns:
      the created component.
      Throws:
      CreateException - if the factory is not able to create the component.