This question concerns design patterns in Python and is addressed to the software designers.
I have several classes inherited from the same abstract class (all they have a similar interface, but the member functions have very different implementations). I want to make a class (or something else) that combines (wraps) all of them. By combining I mean a pattern that creates and returns an object a certain class depending on the input parameters. The most trivial solution is a Python function:
def fabric_function(arg):
if isinstance(arg, float):
return Class1(arg)
if isinstance(arg, str):
return Class2(arg)
Is there a better pattern available to do it? I would really prefer it to be a class that returns either object of Class1
or Class2
depending on parameters. I want the user to make an instance of a class Class
that operates as an instance of the Class1
or Class2
depending on input parameters so the user does not have to make a decision on which of them to use and the output from type(obj)
should be Class
in all cases. In this case, I could modify the member function __new__
responsible for the object making process, but I am not sure it is a clean way to do it. Maybe I should use multiple inheritance and decide in the constructor which parent class to inherit for real?
Aucun commentaire:
Enregistrer un commentaire