views:

172

answers:

3

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 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.

+2  A: 

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.

SLaks
+1, but because of the new unicorn
Nick Craver
+4  A: 

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();
    }
}
John Saunders
@Reed: @dboarman: thanks for catching the typo.
John Saunders
A: 

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.

Benjamin Podszun