r/javahelp • u/thechexmo • Jul 21 '20
Are there meaningful differences between Java observers, listeners and events?
I'm studying design patterns (self taught using "head first design patterns" book) and recently got into Observer patterns... The book says some things about Java out-of-the-box Observer/Observable, but it's not fully clear about what i am asking for... Maybe is a stupid question, but i want to dive deeper into this. Thanks.
20
Upvotes
3
u/morhp Professional Developer Jul 22 '20
It's the same concept, but there can be minor differences.
Observers usually observe changes of some value or state. Listeners don't necessarily need to listen for a state change (although there are change listeners), you can also listen to stateless events such as a button click. You probably wouldn't say you create an mouse observer (although you're still using the observer pattern), you'd call that a mouse listener.
And when you're talking about events you usually mean event objects that store some detail about a specific event, for example a mouse click event stores the object that's clicked on, the mouse position, the mouse button that was used and so on.
Observers for value changes often don't generate these details.