I’m currently writing a little C# library to simplify implementing little physical simulations/experiments.
The main component is a SimulationForm that runs a timer-loop internally and hides boilerplate-code from the user. The experiment itself will just be defined through three methods:
Init()(Initialize everything)Render(Graphics g)(Render current simulation state)Move(double dt)(Move experiment fordtseconds)
I just wondered what is the better choice for letting the user implement these functions:
1) Virtual methods to be overridden by the inheriting form
protected virtual void Init() {}
...
or
2) Events
public event EventHandler<MoveSimulationEventArgs> Move = ...
...
Edit: Note that the methods should not be abstract anyway. In fact, there are even more and none of them has to be implemented. It’s often convenient to leave them out since many simulations don’t need them.
The cool thing about this being a “normal form” is that you can write
partial class frmMyExperiment : SimulationForm {
}
and you’re perfectly able to interact with the designer and all inherited controls and settings/properties. I don’t want to lose this features by having a completely different approach.
I prefer using virtual methods in this case.
Also, if the methods are required in order to function, you can make your class an abstract class. This is the best in terms of usability, since it causes the compiler to enforce the usage. If the user tries to use your class/Form without implementing the methods, the compiler will complain.
Events would be a bit inappropriate here, since this really isn’t something where you want more than a single implementation (events allow multiple subscribers), and it, conceptually, is more of a functional of the object, and less a notification caused by the object.