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.

Filter by
Sorted by
Tagged with
282 votes
7 answers
192k views

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 ...
the_critic's user avatar
  • 12.8k
216 votes
10 answers
179k views

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 ...
Ravi's user avatar
  • 31.2k
207 votes
7 answers
158k views

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 ...
modulaaron's user avatar
  • 2,856
197 votes
5 answers
52k views

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 ...
Jess's user avatar
  • 24.5k
175 votes
6 answers
82k views

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's user avatar
  • 1,974
167 votes
3 answers
67k views

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 ...
JohnnyLambada's user avatar
143 votes
8 answers
162k views

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 ...
Deniz Dogan's user avatar
118 votes
8 answers
39k views

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 ...
AnotherDeveloper's user avatar
91 votes
6 answers
120k views

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 ...
neydroydrec's user avatar
  • 7,123
85 votes
12 answers
57k views

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 ...
Carven's user avatar
  • 15.3k
78 votes
2 answers
21k views

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 ...
eonil's user avatar
  • 84.9k
77 votes
14 answers
19k views

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. ...
pdeva's user avatar
  • 44.5k
76 votes
11 answers
29k views

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 { ...
Roger Lipscombe's user avatar
68 votes
9 answers
26k views

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? ...
elado's user avatar
  • 8,620
60 votes
6 answers
46k views

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 ...
mfrachet's user avatar
  • 8,842
59 votes
14 answers
58k views

How to combine two live data one after the other?

I have next use case: User comes to registration form, enters name, email and password and clicks on register button. After that system needs to check if email is taken or not and based on that show ...
clzola's user avatar
  • 1,975
56 votes
9 answers
59k views

Python Observer Pattern: Examples, Tips? [closed]

Are there any exemplary examples of the GoF Observer implemented in Python? I have a bit code which currently has bits of debugging code laced through the key class (currently generating messages to ...
Jim Dennis's user avatar
  • 17.3k
56 votes
5 answers
44k views

Text change notification for an NSTextField

I would like to use the code from the answer to this question: How to observe the value of an NSTextField on an NSTextField in order to observe changes on the string stored in the NSTextField. [[...
alecail's user avatar
  • 4,022
51 votes
5 answers
27k views

MediatorLiveData or switchMap transformation with multiple parameters

I am using Transformations.switchMap in my ViewModel so my LiveData collection, observed in my fragment, reacts on changes of code parameter. This works perfectly : public class MyViewModel extends ...
Yann39's user avatar
  • 15.1k
50 votes
3 answers
32k views

Why should the observer pattern be deprecated?

I've noticed that my dependency injected, observer-pattern-heavy code (using Guava's EventBus) is often significantly more difficult to debug than code I've written in the past without these features. ...
Jeff Axelrod's user avatar
49 votes
12 answers
31k views

Data Pull vs. Push OOP Approach

When I design my system from scratch, I often face a dilemma whether my object should push information into another objects OR whether the objects should pull the necessary data from another objects. ...
Bunkai.Satori's user avatar
46 votes
0 answers
13k views

Publisher-subscriber vs Observer [closed]

I am trying to make sense of Observer design pattern as the main event dispatching design pattern. The Observer pattern appears to be type or kind of Publish-Subscribe design pattern and I am ...
Jermin Bazazian's user avatar
41 votes
5 answers
72k views

How to create custom Listeners in java?

I want to know about how to set our own Listeners in java.For example I have a function that increments number from 1 to 100. i want to set a listener when the value reaches 50. How can i do that? Pls ...
vnshetty's user avatar
  • 20.1k
40 votes
12 answers
12k views

Simple way of turning off observers during rake task?

I'm using restful_authentication in my app. I'm creating a set of default users using a rake task, but every time I run the task an activation email is sent out because of the observer associated ...
MediaJunkie's user avatar
40 votes
2 answers
25k views

How to create custom event in symfony2

I want to create custom events called user_logged so that i can attach my listeners to those events. I want to execute few functions whenever user has logged in.
Mirage's user avatar
  • 31.2k
40 votes
5 answers
34k views

How can I update information in an Android Activity from a background Service

I am trying to create a simple Android application that has a ActivityList of information, when the application starts, I plan to start a Service that will be constantly calculating the data (it will ...
rhinds's user avatar
  • 10k
39 votes
7 answers
79k views

pass function in json and execute

Is there any way that I can pass a function as a json string (conversion with JSON.stringify), send it to another function, parse the json and then execute the function that was in the json? I am ...
amateur's user avatar
  • 44k
38 votes
6 answers
34k views

Callback/Command vs EventListener/Observer Pattern

I'm trying to design an async framework and wanted to know what people think are the pros/cons of the callback pattern vs the observer pattern. Callback pattern: //example callback public interface ...
DD.'s user avatar
  • 21.8k
38 votes
5 answers
19k views

How can I implement the observer pattern in Rust?

I have an observable collection and an observer. I want the observer to be a trait implementation of trait Observer. The observable object should be able to notify each observer when some event occurs....
VP.'s user avatar
  • 16.1k
38 votes
4 answers
13k views

How would you test observers with rSpec in a Ruby on Rails application?

Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications - how do you test this observer with rSpec?
Nicolai Reuschling's user avatar
37 votes
1 answer
8k views

How do I create a Mailer Observer

I'd like to run some code whenever an email is sent on my app. As ActionMailer doesn't support after_filter, I would like to use an observer. The Rails docs mention this in passing, however does not ...
thomasfedb's user avatar
  • 5,998
36 votes
3 answers
39k views

Android Rxjava subscribe to a variable change

I am learning Observer pattern, I want my observable to keep track of a certain variable when it changes it's value and do some operations, I've done something like : public class Test extends ...
Karate_Dog's user avatar
  • 1,277
36 votes
2 answers
8k views

What happened to Scala.React?

I read the paper cowritten by Odersky, "Deprecating the Observer Pattern with Scala.React" The github looks abandoned: https://github.com/ingoem/scala-react Also, the recent Reactive Programming ...
user2684301's user avatar
  • 2,580
35 votes
3 answers
20k views

Observers vs. Callbacks

i thought about using observers or callbacks. What and when you should use an observer? F.e. you could do following: # User-model class User << AR after_create :send_greeting! def ...
BvuRVKyUVlViVIc7's user avatar
35 votes
2 answers
75k views

Implementing a callback in Python - passing a callable reference to the current function

I want to implement the Observable pattern in Python for a couple of workers, and came across this helpful snippet: class Event(object): pass class Observable(object): def __init__(self): ...
malangi's user avatar
  • 2,752
35 votes
5 answers
22k views

C++11 observer pattern (signals, slots, events, change broadcaster/listener, or whatever you want to call it)

With the changes made in C++11 (such as the inclusion of std::bind), is there a recommended way to implement a simple single-threaded observer pattern without dependence on anything external to the ...
learnvst's user avatar
  • 15.9k
33 votes
14 answers
9k views

Problems implementing the "Observer" pattern

I have met an interesting problem while implementing the Observer pattern with C++ and STL. Consider this classic example: class Observer { public: virtual void notify() = 0; }; class Subject { ...
SadSido's user avatar
  • 2,551
32 votes
4 answers
16k views

When should I remove observers? Error about deallocating objects before removing observers

I am trying to use key-value observing in one of my classes. I register the observers in the init method and remove/deregister them in the dealloc, but I get the following error which seems to occur ...
Kaom Te's user avatar
  • 774
31 votes
3 answers
8k views

Rx for .NET - What happened to Scheduler.Dispatcher?

I'm trying to work through Dan Sullivan's Rx Extensions training course on PluralSight. It's excellent stuff but unfortunately Rx seems to have already been changed, even though the course was only ...
irascian's user avatar
  • 1,955
29 votes
4 answers
9k views

How to implement the Observer Design Pattern in a pure functional way?

Let's say I want to implement an event bus using a OO programming language. I could do this (pseudocode): class EventBus listeners = [] public register(listener): listeners.add(...
ivo's user avatar
  • 4,191
28 votes
4 answers
16k views

Is an EventListener an Observable?

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 ...
Daan's user avatar
  • 432
27 votes
3 answers
30k views

Creating an Observable for Mock Data in Angular 2

I am trying to return an Observable from a service with mock data. I am returning this from my service : return Observable.of(new Object()).map(MOCKACCOUNT =>JSON.stringify(MOCKACCOUNT)); I get ...
Chris's user avatar
  • 1,309
26 votes
14 answers
38k views

Finding ResizeObserver with Typescript in Angular 9

I am trying to implement a ResizeObserver in an Angular app: const obs = new ResizeObserver(e => { // ... }); ... and am met with the TS error: TS2304: Cannot find name 'ResizeObserver'. ...
jared jessup's user avatar
26 votes
1 answer
16k views

Rails 3 Observer -- looking to learn how to implement an Observer for multiple models

I'd like to add an Auditor Observer which does an action anytime after_create for 3 models (books, characters, authors)... I recently heard of the Observer capability but can't find any documentation ...
AnApprentice's user avatar
26 votes
2 answers
57k views

C# delegate v.s. EventHandler

I want to send an alert message to any subscribers when a trap occurred. The code I created works fine using a delegate method myDelegate del. My questions are: I want to know whether it's better ...
Hsu Wei Cheng's user avatar
24 votes
5 answers
35k views

RxJava timer that repeats forever, and can be restarted and stopped at anytime

In android i use Timer to execute task that repeats every 5 seconds and starts after 1 second in this way: Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @...
MBH's user avatar
  • 16.5k
24 votes
6 answers
15k views

A generic observer pattern in Java

The java.util.Observer and java.util.Observable are ugly. They require the sorts of casts that make type-safety fans uncomfortable, and you can't define a class to be an Observer of multiple things ...
hcarver's user avatar
  • 7,156
24 votes
5 answers
10k views

What is the opposite of the observer pattern?

As I understand it, the observer pattern allows for multiple observers to monitor a single subject. Is there a pattern for the opposite scenario? Is there a pattern for a single observer that ...
blueshift's user avatar
  • 861
24 votes
6 answers
7k views

Design pattern for implementing plugins in PHP applications

Is there a consensus on how plugins should be implemented in a PHP application? I've looked into the observer pattern which comes close, it's really just a notification system and doesn't allow code ...
Elbert Alias's user avatar
  • 1,790
21 votes
6 answers
10k views

Observe a File or Folder in Objective-C

What is the best way to listen to a folder or file to see if it has been saved or if a new file has been added?
Garrett's user avatar
  • 7,940

1
2 3 4 5
37