Class Factory

java.lang.Object
org.pipservices3.components.build.Factory
All Implemented Interfaces:
IFactory
Direct Known Subclasses:
DefaultCacheFactory, DefaultConfigReaderFactory, DefaultCountersFactory, DefaultCredentialStoreFactory, DefaultDiscoveryFactory, DefaultInfoFactory, DefaultLoggerFactory

public class Factory
extends Object
implements IFactory
Basic component factory that creates components using registered types and factory functions.

#### Example ###

 
 Factory factory = new Factory();
 
 factory.registerAsType(
 		new Descriptor("mygroup", "mycomponent1", "default", "*", "1.0"),
 		MyComponent1.class
 );
 factory.register(
 		new Descriptor("mygroup", "mycomponent2", "default", "*", "1.0"),
 		(locator) -> {
 			return new MyComponent2();
 		}
 );
 
 factory.create(new Descriptor("mygroup", "mycomponent1", "default", "name1", "1.0"))
 factory.create(new Descriptor("mygroup", "mycomponent2", "default", "name2", "1.0"))
 
 
See Also:
Descriptor, IFactory
  • Constructor Details

    • Factory

      public Factory()
  • Method Details

    • register

      public void register​(Object locator, Factory.IComponentFactory factory)
      Registers a component using a factory method.
      Parameters:
      locator - a locator to identify component to be created.
      factory - a factory function that receives a locator and returns a created component.
    • registerAsType

      public void registerAsType​(Object locator, Class<?> type)
      Registers a component using its type (a constructor function).
      Parameters:
      locator - a locator to identify component to be created.
      type - a component type.
    • 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.