I am currently following a class about Design Patterns and was wondering whether an EventListener
is an Observable
?
I don't really see a difference between them because both have a list of subscribers and notify these subscribers when something has changed.
"A listener is essentially waiting for an event to occur on a given object, which is what an observer does" not exactly, according to the DP book observers don't wait for any kind of event, but a change in the state of the subject, which then the observer can query for (pull).
An event listener is a procedure or function in a computer program that waits for an event to occur. Examples of an event are the user clicking or moving the mouse, pressing a key on the keyboard, disk I/O, network activity, or an internal timer or interrupt.
Often an event listener is registered with the object that generates the event. When the event occurs, the object iterates through all listeners registered with it informing them of the event.
What Is the Observer Pattern? Observer is a behavioral design pattern. It specifies communication between objects: observable and observers. An observable is an object which notifies observers about the changes in its state. For example, a news agency can notify channels when it receives news.
An Observable
is simply an object where you can observe it's actions. So anything where you can listen to an action and then be told that action occurs is an Observable
.
This means an Event Listener is one. Because you can listen to events and the events immediately notify you that they have happened.
Personally when anyone says Observable
I think events. This is my cookie cutter example of what observables are. A similar example would be a publish-subscribe system which is just events under a different name (it does have subtly different use case).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With