My Python knowledge is limited, I need some help on the following situation.
Assume that I have two classes A
and B
, is it possible to do somet
Coming from the worlds of Java and C#, the thought of doing something like this makes me cringe =P. (Not that the idea of conditionally inheriting a class is bad - I'm just not used to it.)
Thinking about it for a bit, I would have done something like this if this question were about Java. I'm posting the pattern - implement an interface, then use a factory to select between the implementations - in order to provide another perspective to the problem:
public interface OsDependent {
public void doOsDependentStuff();
}
public class WindowsDependentComponent implements OsDependent {
@Override
public void doOsDependentStuff() {
//snip
}
}
public class AppleDependentComponent implements OsDependent {
@Override
public void doOsDependentStuff() {
//snip
}
}
public class OsDependentComponentFactory {
public OsDependent getOsDependentComponent(Platform platform) {
if(platform == Platform.WINDOWS)
return new WindowsDependentComponent();
else if(platform == Platform.APPLE)
return new AppleDependentComponent();
else
return null;
}
}
Definitely a lot more code, but it's an appropriate solution in a strongly typed environment.
EDIT: One significant difference I noticed between my answer and the original question:
If you conditionally inherit from multiple different classes, then the superclasses contain code that depend on which OS you're using, while the class that inherits from them contains code that is the same for all OS's. The top of the inheritance chain is OS-dependent; the bottom isn't.
My approach goes the other way. The OsDepndent
interface (or superclass) defines methods that are similar for all platforms, while the different implementations (or subclasses) have OS-dependent code. The top of the inheritance chain is OS-agnostic.