I was wondering what are the pros and cons for using either events or interface, as both can do the same job. So, instead of the call to the action, you would get all components with a certain interface and call their method/s.
Each serves a slightly different purpose…
Events are agnostic about callers… you can have any other class subscribing to the event, and when the event is invoked, that classes delegate will get called. Events are for situations where the class invoking the event has no idea who has subscribed. It’s a bit like joining an email list.
Interfaces are for situations where you want to communicate with everybody who meets a certain criteria… It’s more discriminating than events, as obviously the class must have the interface, but it’s also less discriminating in that as you’re describing, every class implementing that interface will get the call.
Brian, thanks for the answer. So technically they can be interchanged, but context is important. For example in the rpg course ISaveable is used and you could technically make it so that it works with events instead, but with interfaces you know exactly what you must do in order to save (capture state and restore state), while with events it’s a lot more losely coupled, you can do anything with the subscribed methods.