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.
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()Although it supports the subclass search of a given interface, I find it more effective to explicitly register the implementations at runtime.