I have a class A
which implements method x()
and y()
but I also have a class B
which implements method z()
. Assuming there is a base AbstractA
class. how can I detect any calls on class A
which aren't implemented e.g. z()
and forward them to class B
? Please note that I can't have A
inherit from B
due to framework plumbings i.e.,
from abc import ABC, abstractmethod
class AbstractA(ABC):
# some magic for catching unimplemented method calls e.g. z
# and forward them to B's. Here I have access to instances of
# B e.g. context.b.z()
@abstractmethod
def x():
pass
@abstractmethod
def y():
pass
class A(AbstractA):
def __init__(self):
super().__init__()
def x():
print('running x()')
def y():
print('running y()')
class B:
def __init__(some plumbing args):
super().__init__(some plumbing args)
def z():
print('running z()')
a = A()
a.x()
a.y()
a.z()
To give a bit of context on this use-case, I have a multi-layered architecture with a data access layer (DAL) and then a service application layer (SAL). The DAL is a collection of DAOs that take care of wrapping all database access use-cases. The SAL builds on top of the DAL and mash ups DAL's data plus business application logic.
For example, a PersonDao
implementation and a PersonService
. PersonService
will call PersonDao
to build the business logic API but some times client code may request PersonService
to find a person by id which is implemented in PersonDao
directly. Therefore, instead of explicitly implementing a pass through service method for each DAO method, have this pass through or delegation automated on the abstract base level of PersonService
so that if you do person_service.find_by_id(3)
it will go straight to PersonDao#find_by_id(id)
's effectively making the service implementation a facade to the underlying DAOs.