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.
1,834
questions
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 ...
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 ...
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 ...
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 ...
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?
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
...
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
{
...
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? ...
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 ...
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 ...
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 ...
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.
[[...
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 ...
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. ...
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.
...
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 ...
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 ...
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 ...
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.
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 ...
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 ...
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 ...
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....
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?
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 ...
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 ...
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 ...
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 ...
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):
...
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 ...
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 {
...
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 ...
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 ...
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(...
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 ...
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 ...
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'.
...
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 ...
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 ...
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() {
@...
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 ...
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 ...
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 ...
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?