pip_services3_components.build package

Submodules

Module contents

pip_services3_components.build.__init__

Contains the “factory design pattern”. There are various factory types, which are also implemented in a portable manner.

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_components.build.CompositeFactory(*factories: pip_services3_components.build.IFactory.IFactory)

Bases: pip_services3_components.build.IFactory.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:

factory = CompositeFactory()
factory.add(new DefaultLoggerFactory())
factory.add(new DefaultCountersFactory())

loggerLocator = Descriptor("*", "logger", "*", "*", "1.0")
factory.can_create(loggerLocator)  # Result: Descriptor("pip-service", "logger", "null", "default", "1.0")
factory.create(loggerLocator)      # Result: created NullLogger
add(factory: pip_services3_components.build.IFactory.IFactory)

Adds a factory into the list of embedded factories.

Parameters

factory – a factory to be added.

can_create(locator: Any) → Any

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.

Parameters

locator – a locator to identify component to be created.

Returns

a locator for a component that the factory is able to create.

create(locator: Any) → Any

Creates a component identified by given locator.

Parameters

locator – a locator to identify component to be created.

Returns

the created component.

remove(factory: pip_services3_components.build.IFactory.IFactory)

Removes a factory from the list of embedded factories.

Parameters

factory – the factory to remove.

exception pip_services3_components.build.CreateException(correlation_id: Optional[str] = None, message_or_locator: str = None)

Bases: pip_services3_commons.errors.InternalException.InternalException

Error raised when factory is not able to create requested component.

class pip_services3_components.build.Factory

Bases: pip_services3_components.build.IFactory.IFactory

Basic component factory that creates components using registered types and factory functions.

Example:

factory = Factory()

factory.registerAsType(Descriptor("mygroup", "mycomponent1", "default", "*", "1.0"), MyComponent1)

factory.create(Descriptor("mygroup", "mycomponent1", "default", "name1", "1.0"))
can_create(locator: Any) → Any

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.

Parameters

locator – a locator to identify component to be created.

Returns

a locator for a component that the factory is able to create.

create(locator: Any) → Any

Creates a component identified by given locator.

Parameters

locator – a locator to identify component to be created.

Returns

the created component.

register(locator: Any, factory: Any)

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.

register_as_type(locator: Any, component_type: Any)

Registers a component using its type (a constructor function).

Parameters
  • locator – a locator to identify component to be created.

  • component_type – a component type.

class pip_services3_components.build.IFactory

Bases: object

Interface for component factories.

Factories use locators to identify components to be created.

The locators are similar to those used to locate components in references. They can be of any type like strings or integers. However Pip.Services toolkit most often uses Descriptor objects as component locators.

can_create(locator: Any) → Any

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.

Parameters

locator – a locator to identify component to be created.

Returns

a locator for a component that the factory is able to create.

create(locator: Any) → Any

Creates a component identified by given locator.

Parameters

locator – a locator to identify component to be created.

Returns

the created component.