Answer the question
In order to leave comments, you need to log in
Is this factory method implementation bad?
Is this factory method implementation bad? Why? And how can it be better?
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]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question