Questions tagged [observer-pattern]

A design pattern in which an object, called the subject, maintains a list of its dependents, called observers and notifies them automatically of any state changes, usually by calling one of their methods. It is one of the Gang of Four's behavioral design patterns. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

The Observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

This is one of the Gang of Four's behavioral , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".


1815 questions
7 answers

Delegation: EventEmitter or Observable in Angular

I am trying to implement something like a delegation pattern in Angular. When the user clicks on a nav-item, I would like to call a function which then emits an event which should in turn be handled by some other component listening for the…
  • 12,720
  • 19
  • 67
  • 115
10 answers

When should we use Observer and Observable?

An interviewer asked me: What is Observer and Observable and when should we use them? I wasn't aware of these terms, so when I got back home and started Googling about Observer and Observable, I found some points from different resources: 1)…
  • 30,829
  • 42
  • 119
  • 173
7 answers

Determine what attributes were changed in Rails after_save callback?

I'm setting up an after_save callback in my model observer to send a notification only if the model's published attribute was changed from false to true. Since methods such as changed? are only useful before the model is saved, the way I'm currently…
  • 2,856
  • 3
  • 24
  • 28
5 answers

Difference between Observer, Pub/Sub, and Data Binding

What is the difference between the Observer Pattern, Publish/Subscribe, and Data Binding? I searched around a bit on Stack Overflow and did not find any good answers. What I have come to believe is that data binding is a generic term and there are…
6 answers

Observer is deprecated in Java 9. What should we use instead of it?

Java 9 came out, and Observer has been deprecated. Why is that? Does it mean that we shouldn't implement observer pattern anymore? It would be good to know what is a better alternative?
  • 1,904
  • 3
  • 15
  • 17
3 answers

Observer Design Pattern vs "Listeners"

It seems to me that the Observer design pattern as described in GoF is really the same thing as Listeners found in various toolkits. Is there a difference between the concepts, or are Listeners and Observers really the same thing. (I'm not looking…
  • 12,700
  • 11
  • 57
  • 61
8 answers

Super-simple example of C# observer/observable with delegates

I recently started digging into C# but I can't by my life figure out how delegates work when implementing the observer/observable pattern in the language. Could someone give me a super-simple example of how it is done? I have googled this, but all…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
8 answers

Mediator Vs Observer Object-Oriented Design Patterns

I have been reading the Gang Of Four, in order to solve some of my problems and came across the Mediator pattern. I had earlier used Observer in my projects for making some GUI application. I am a bit confused as I do not find great difference…
  • 2,161
  • 2
  • 23
  • 27
6 answers

How to trigger function on value change?

I realise this question has to do with event-handling and i've read about Python event-handler a dispatchers, so either it did not answer my question or i completely missed out the information. I want method m() of object A to be triggered whenever…
  • 6,973
  • 9
  • 57
  • 89
12 answers

Difference between Observer Pattern and Event-Driven Approach

I always found the Observer Pattern almost similar to the usual event-driven approach. Actually, I have almost believed that they are actually just different names referring to the same thing. They both use similar concepts to have something as a…
  • 14,988
  • 29
  • 118
  • 161
14 answers

Pros and Cons of Listeners as WeakReferences

What are the pros and cons of keeping listeners as WeakReferences? The big 'Pro' of course is that: Adding a listener as a WeakReference means the listener doesn't need to bother 'removing' itself. For those worried about the listener having the…
  • 43,605
  • 46
  • 133
  • 171
11 answers

C#: events or an observer interface? Pros/cons?

I've got the following (simplified): interface IFindFilesObserver { void OnFoundFile(FileInfo fileInfo); void OnFoundDirectory(DirectoryInfo directoryInfo); } class FindFiles { IFindFilesObserver _observer; // ... } ...and I'm…
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
2 answers

What is difference between observer pattern and reactive programming?

Recently I heard the term reactive programming a lot, but when I searched for it, what I discovered was only some similarities with observer pattern. Actually, I cannot find any differences between them. What's conceptual difference between them and…
  • 83,476
  • 81
  • 317
  • 516
9 answers

Rails 3: How to identify after_commit action in observers? (create/update/destroy)

I have an observer and I register an after_commit callback. How can I tell whether it was fired after create or update? I can tell an item was destroyed by asking item.destroyed? but #new_record? doesn't work since the item was saved. I was going…
  • 8,510
  • 9
  • 51
  • 60
6 answers

How does AngularJS know when variables change? How does AngularJS dirty checking work?

I was reading some article to understand a little bit more how AngularJS works. One of the terms that I didn't understand is Dirty Checking. What is it exactly? It seems like the Observer pattern but apparently it's better. Can you help me…
  • 8,772
  • 17
  • 55
  • 110
2 3
99 100