I’ve noticed in a lot of Microsoft .NET classes that there’s often a non-abstract/virtual public method, such as
public bool MyAwesomeMethod(object someParameter);
And a protected abstract/virtual method such as
protected virtual bool OnMyAwesomeMethod(object someParameter);
I recently started to wonder whether this was a known design pattern or not and what the advantages and disadvantages of such a design might be. Is it all about enabling the base class to execute some logic that children of the class can’t prevent (such as logging)? Is it undesirable for some reason to use this sort design by default for methods you know you want to be overridable in child classes? Are there other considerations I’m not picking up on?
Thanks in advance for your help.
That’s pretty much it; you use it when you want some behaviour to always happen before and/or after the derived classes’ override.
Logging is certainly possible, but unlikely in the more low-level calls that it is sometimes done with. It could be one of several things though, which might be vital to the class’ operation.
It could also be done “just in case” such trapping of each call before or after is needed later. Normal flexibility vs YAGNI arguments apply.