When developing an event, is it recommended to prevent the addition of listeners twice?

I am creating an event-based API where a user can subscribe to an event by adding listener objects (as is customary in Java or C #). When an event is raised, all subscribers-subscribers are called with information about the event.

At first, I decided to prevent the addition of an event listener several times. If a listener is added that already exists in the listener collection, it is not added again. However, after thinking about it a little more, it seems that most event-based structures actually prevent this. Was my initial instinct wrong? I'm not sure where to go. I think I thought that preventing the addition of an existing listener would help avoid a common programming error. Again, it can also hide an error that will cause code to run several times when it shouldn't.

+3
source share
3 answers

, , , , . Java Observable , CopyOnWriteArrayList , (addIfAbsent()). , - , . ( ), , .

+1

, . , () - . , .

, , , ?

, , " " .

+2

, , , . , , , . Java Observable , Observer , addObserver , Observer no-op. , Python , , .

+2

Source: https://habr.com/ru/post/1749858/


All Articles