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".

References:

1727 questions
264
votes
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…
the_critic
  • 12,014
  • 19
  • 60
  • 109
204
votes
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)…
Ravi
  • 28,657
  • 41
  • 110
  • 158
170
votes
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…
modulaaron
  • 2,476
  • 3
  • 20
  • 27
169
votes
4 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…
152
votes
5 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?
curious95
  • 1,719
  • 3
  • 13
  • 16
149
votes
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…
JohnnyLambada
  • 11,732
  • 10
  • 52
  • 59
136
votes
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
  • 23,833
  • 33
  • 104
  • 154
98
votes
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…
HumbleDeveloper
  • 1,864
  • 1
  • 19
  • 24
74
votes
12 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…
pdeva
  • 36,445
  • 42
  • 122
  • 154
73
votes
5 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…
neydroydrec
  • 5,701
  • 9
  • 41
  • 77
71
votes
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…
Carven
  • 12,832
  • 24
  • 97
  • 139
64
votes
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…
eonil
  • 75,400
  • 74
  • 294
  • 482
63
votes
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…
elado
  • 7,678
  • 9
  • 45
  • 56
62
votes
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
  • 81,986
  • 49
  • 214
  • 348
59
votes
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…
mfrachet
  • 7,764
  • 12
  • 51
  • 95
1
2 3
99 100