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:

1815 questions
37
votes
4 answers

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
  • 2,558
  • 2
  • 20
  • 24
36
votes
3 answers

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 MyChildActivity { private int VARIABLE_TO_OBSERVE =…
Karate_Dog
  • 1,265
  • 5
  • 20
  • 37
36
votes
2 answers

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 Coursera class, used the JavaRx Observable library…
user2684301
  • 2,550
  • 1
  • 24
  • 33
35
votes
2 answers

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): self.callbacks = [] def subscribe(self,…
35
votes
3 answers

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 send_greeting! UserNotifier.deliver_greeting_message(self) …
BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111
35
votes
5 answers

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 core language or standard library (like…
learnvst
  • 15,455
  • 16
  • 74
  • 121
33
votes
14 answers

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 { public: void addObserver( Observer* ); void…
SadSido
  • 2,511
  • 22
  • 36
32
votes
4 answers

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 before my dealloc method gets called, according to…
31
votes
3 answers

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 published a month ago. Most of the changes are…
irascian
  • 1,945
  • 2
  • 15
  • 9
28
votes
4 answers

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 subscribers and notify these subscribers when something…
Daan
  • 432
  • 1
  • 7
  • 17
27
votes
4 answers

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(listener) public unregister(listener): …
ivo
  • 4,101
  • 5
  • 33
  • 42
27
votes
3 answers

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 an error Observable_1.Observable.of is not a…
Chris
  • 1,299
  • 3
  • 18
  • 34
26
votes
1 answer

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 on the ability. Is it support in Rails 3? How do…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
25
votes
14 answers

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'. I've tried to update my type definition…
jared jessup
  • 629
  • 2
  • 7
  • 11
24
votes
6 answers

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 to extend the application directly. I'm currently…
Elbert Alias
  • 1,770
  • 1
  • 16
  • 25