Can A Constructor Return a SubClass?
Given the following client code:
var obj = new Class1();
Is there any way to modify the constructor of Class1 so that it will actually return a subclass (or some other alternate implementation) instead?
I would like obj to get one of two different implementations, depending on some condition. Obviously, I could change to using a factory or DI framework, but开发者_C百科 I'd like to avoid changing the client code, if possible.
I assume the answer is no, but I wonder if there's some clever way of making that happen.
You can replace the constructor with a factory method, and return whatever you like, depending on the parameters:
public Class2 : Class1 {}
public static Class1 CreateClass1(bool returnDerivedClass)
{
if (returnDerivedClass)
{
return new Class2();
}
else
{
return new Class1();
}
}
This is not possible.
Clever workarounds include replacing the constructor with a static
function or (not recommended) using a wrapper around the base class, and creating different wrapped classes.
For things like these you want to check out the Factory pattern. In addition, depending on your needs, I'd recommend looking at general methods to reduce coupling. Your call to the constructor of the class is the hardest coupling you can have in a program and makes things like swapping the implementation out a hassle, as you found out yourself.
Read about "Inversion of control" and "Dependency Injection", maybe that's what you really look for.
A nice library can be found here.
Use an injection library. The link below presents a great list of injection libraries out there and comparison of their performance
http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison
I would suggest however that performance is rarely a criteria you would choose an injection library on, most applications wouldn't instantiate the number of objects this guy has used in his tests. I'm using NInject which does the job well, however if I were to start another project I'd probably give simpleinjector a go, it seems to have all the features I use in NInject and it does well in the performance comparisons.
精彩评论