A good example of observer design pattern | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

A good example of observer design pattern

Can anyone share a code reference to describe good use case of observer design pattern ? I am into c++

2nd Mar 2020, 7:32 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 4
Not a C++ example but if you have seen web code even once in your life, you will probably know window.addEventListener('click', function () { ... }); And so every time the user clicks something, a message is sent to every observer that is willing to listen! What's great is that the Observer doesn't need to do much, it just calls .listen on the Observable. And the Observable doesn't need to do much either, it just walks through an array of Observables and calls them. It's an easy way to make anything talk to anything.
2nd Mar 2020, 8:53 PM
Schindlabua
Schindlabua - avatar
+ 3
Event listeners use the observer pattern, usually used with UI, among other stuff, pretty much as ~swim~ described. Sadly I can't find any good examples right off the bat. :(
2nd Mar 2020, 8:50 PM
Dennis
Dennis - avatar