Является ли данная реализация фабричного метода плохой? Почему? И как можно лучше?
def inheritors(cls: Class) -> List[Class]:
"""Get all class inheritors
Returns:
List[Class]: return all subclasses
"""
subclasses = []
work = [cls]
while work:
parent = work.pop()
for child in parent.__subclasses__():
if child not in subclasses:
subclasses.append(child)
work.append(child)
return subclasses
class NodeForm(ABC):
CONTENT_TYPE = None
# некоторые поля и методы
@abstractmethod
def some_operation(self) -> str:
pass
@classmethod
def get_all_subforms(cls) -> Dict[str, object]:
out = dict()
for child_cls in inheritors(cls):
try:
out[child_cls.CONTENT_TYPE] = child_cls
except Exception:
pass
return out
class TextNodeForm(NodeForm):
CONTENT_TYPE = "text"
def some_operation(self):
return "TextNodeForm"
class VideoNodeForm(NodeForm):
CONTENT_TYPE = "video"
def some_operation(self):
return "TextNodeForm"
def NodeFormFactory(content_type: str) -> NodeForm:
return NodeForm.get_all_subforms()[content_type]