Can someone tell me if this class structure is bad?
class abstract Parent{
public Child Foo(){
return new Child();
}
}
class Child : Parent{}
I've heard that referring to a derived type from a base type is always bad and signs of bad design. Can someone tell me why this is bad or even if it is bad?
It looks to me like you are using the base class as a factory. I would recommend against this design because it reduces the cohesion of the base class (it's both base class and factory) and increases coupling in the base class (as it references the derived class(es)).
Create a factory class separate from the base class and these issues go away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With