As you can see in the code below, the DoStuff() method is getting called before the Init() one during the construction of a Child object.
I'm in a situation where I have numerous child classes. Therefore, repeating a call to the DoStuff() method directly after Init() in the constructor of each child wouldn't be an elegant solution.
Is there any way I could create some kind of post constructor in the parent class that would be executed after the child's constructor? This way, I could call to the DoStuff() method there.
If you have any other design idea which could solve my problem, I'd like to hear it too!
abstract class Parent
{
public Parent()
{
DoStuff();
}
protected abstract void DoStuff();
}
class Child : Parent
{
public Child()
// DoStuff is called here before Init
// because of the preconstruction
{
Init();
}
private void Init()
{
// needs to be called before doing stuff
}
protected override void DoStuff()
{
// stuff
}
}
Let me introduce a general solution using some C# features. Note that this solution does not require you to use a factory pattern or invoke anything after constructing the object, and it works on any class with just implementing an interface with a single method. First we declare an interface that our classes will have to implement:
Next we add a static extension class for this interface, and add the Initialize method:
Now, if we need a class and all of its descendants to call an initializer right after the object is fully constructed, all we need to do is implement
IInitialize
and append a line in the constructor:The trick is that when a derived class calls the extension method
Initialize
, that will suppress any calls not made from the actual class.As others have mentioned, you should use a Factory Pattern.
Rather than using an
abstract
method, which would require you to implement the method in all descendant classes, you might try: