Given the following classes:
ClassA
{
public ClassA DoSomethingAndReturnNewObject()
{}
}
ClassB : ClassA
{}
ClassC : ClassA
{}
You need to create a protected virtual method for the DoSomethingAndReturnNewObject
to use:
class ClassA
{
protected virtual ClassA Create()
{
return new ClassA()
}
public ClassA DoSomethingAndReturnNewObject()
{
ClassA result = Create();
// Do stuff to result
return result;
}
}
class ClassB : ClassA
{
protected override ClassA Create() { return new ClassB(); }
}
class ClassC : ClassA
{
protected override ClassA Create() { return new ClassC(); }
}
Note the return type remains ClassA but the object instance type will be the specific class.