jeudi 12 août 2021

Correct design pattern of multiple inheritance of abstract class

I have a complex class structure such has this one below:

from abc import ABC, abstractmethod
# Abstract Foo Space
class Foo(ABC):
    def __init__(self, value):
        self.value = value

    @abstractmethod
    def foo_something(self):
        pass

class ChildFoo(Foo):
    def __init__(self, value, extended_foo):
        Foo.__init__(self, value=value)
        self.extended_foo = extended_foo

    def foo_something(self):
        print(self.extended_foo)



# Abstract Bar Space, extend Foo
class Bar(Foo, ABC):
    def __init__(self, value, extended_bar):
        Foo.__init__(self, value=value)
        self.extended_bar = extended_bar

    @abstractmethod
    def bar_something(self):
        pass


class ChildBarFoo(Bar, ChildFoo):
    def __init__(self, value, extended_foo, extended_bar):
        Bar.__init__(self, value=value, extended_bar=extended_bar)
        ChildFoo.__init__(self, value=value, extended_foo=extended_foo)

    def bar_something(self):
        print(self.extended_bar)

my_object = ChildBarFoo(value=100, extended_foo='foo', extended_bar='bar')
my_object.bar_something()
my_object.foo_something()

When I instantiate ChildBarFoo, Foo will be initiated twice, once from ChildFoo and once from Bar which makes me wonder if the pattern I am using is correct.

Aucun commentaire:

Enregistrer un commentaire