I'm writing a Java program with a custom event manager, which is of course the observer pattern. Should the calls to add event listeners/handlers be in the classes that listens/handles the events such as in their constructor, or outside them such as near the code that calls their constructor?
For example, say there's a class that creates a new user and raises an event that a new user has been created. Should the class that sends welcome emails to new users register itself as an event handler, perhaps in its constructor? Or, should that registration not happen within the class that will handle the event?
I'll point out there are situations where instances of a class need to handle separate events. For example, a class handling UI button clicks that registered itself in its constructor would need to be given the event in its constructor to know which to listen to.
It seems like having the class register itself violates the single responsibility principle. It also forces some coupling for the class to know the events it needs to respond to, especially when there's multiple similar events that all get handled the same way. When more similar events are added, it forces the class registering itself to be changed. Of course, registering outside the class forces those changes also, but it's at least not within that class.
However, I don't love the idea of registering listeners to events outside the classes handling the events, because that seems to make it more likely the registration could be forgotten.