I'm refactoring some code which isn't very reusable and has quite a bit of duplicate code. The code has two classes A and B which extend the abstract class I. But there are subclasses of A and B to support concepts X and Y so the result is concrete classes AX, AY, BX, BY with concepts X and Y copy and pasted into each.
So I know I can use composition here to delegate support for features X and Y but this also requires code that builds these objects etc which is why I started reading about mixins, so I'm wondering if my code is a good solution
class I(ABC):
@abstractmethod
def doSomething():
pass
class ICommon(ABC):
@abstractmethod
def doSomethingCommon():
pass
class A(I, ICommon):
# the interface(s) illustrates what mixins are supported
# class B could be similar, but not necessarily with the same interfaces
def doSomething():
self.doSomethingCommon()
...
class XCommonMixin(object):
# feature X shared possibly in A and B
# I have also split features X into much smaller concise parts,
# so the could be a few of these mixins to implement the different
# features of X
def doSomethingCommon():
return 42
class AX(XCommonMixin, A):
pass
# init can be defined to construct A and bases if any as appropriate