A subclass needs to know when particular events occur withing its superclass, but there are more than one ways for the superclass to break the news. Here are 2:
- dispatch an event
- call an abstract method which the subclass could eventually override
I was wondering if best practices would recommend one of the approaches over the other.
P.S. I was working with ActionScript when I thought of this question.
That depends on the nature of the problem you’re trying to solve. Does the processing done by the subclass need to be asynchronous? If so, that is an argument in favor of an event-based design. Could things that don’t subclass from your class want to know about it? That’s another argument in favor of an event-based design. Other than that, it’s probably easiest to provide an abstract method that serves as a hook into your logic, because that’s what a lot (most, in my experience) of programmers would expect.