Skip to content

Jonathan-Mckenzie/service-locator-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Service Locator Python

Proof of concept by implementing the Service Locator Pattern using Abstract Base Class in Python

The goal is to have a single service that provides the implementation of any specified interface.

Example

from abc import ABC, abstractmethod

class AuthInterface(ABC):
  @abstractmethod
  def verify(self):
    pass

class OAuth2(AuthInterface):
  def verify(self):
    print("verifying auth w/ oauth2 protocol...")

# instantiate service locator
service_locator = ServiceLocator()

# register auth to be performed by OAuth2
service_locator.register_service(AuthInterface, OAuth2)

# get the auth implementation
auth_service = service_locator.get_service(AuthInterface)

# execute provided implementation, prints: "verifying auth w/ oauth2 protocol..."
auth_service.verify()

Note

Although it supports the subclass search of a given interface, I find it more effective to explicitly register the implementations at runtime.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages