Skip to content

Singleton Registry #68

Open
Open
@kozlov721

Description

@kozlov721

Singleton Registry Pattern

In the current implementation, the Registry have to be first instantiated somewhere and the instance have to be then imported whenever one wants to use the registry.

Disadvantages

It might not be always clear where should one define the Registry instances. If defined in wrong place, it can cause issues with circular imports or similar import errors.

Solution

It would be advantageous for the registry instances to act as singletons based on the provided names.
That would mean that every time a registry with the same name is instantiated, the firstly created instance is returned instead of creating a new empty registry. This will make the registries a bit more convenient to use and avoids any possible import errors.

Current Method

# ---- foo.py ---- 

from luxonis_ml.utils import Registry

REGISTRY = Registry(name="registry_name")

@REGISTRY.register_module()
class Foo:
    pass
# ---- bar.py ----

from foo import REGISTRY

REGISTRY.get("Foo")
# >>> <class '__foo__.Foo'>

Proposed Method

# ---- foo.py ---- 

from luxonis_ml.utils import Registry

# If defined for the first time, creates a global static `Registry` instance.
REGISTRY = Registry("registry_name")

@REGISTRY.register_module()
class Foo:
    pass
# ---- bar.py ----

# no need to import REGISTRY from foo.py
from luxonis_ml.utils import Registry

Registry("registry_name").get("Foo")
# >>> <class '__foo__.Foo'>

To Consider

  • Should the names be case sensitive or not?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions