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
57
votes
13 answers

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 error message or create new user... I am trying to…
56
votes
5 answers

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. [[NSNotificationCenter defaultCenter] …
alecail
  • 3,993
  • 4
  • 33
  • 52
54
votes
9 answers

Python Observer Pattern: Examples, Tips?

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 stderr if a magic env is set). Additionally, the…
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
51
votes
5 answers

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 AndroidViewModel { private final…
49
votes
3 answers

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. Particularly when trying to determine when and…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
48
votes
12 answers

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. Is there anything like a standard in OOP design,…
Bunkai.Satori
  • 4,698
  • 13
  • 49
  • 77
46
votes
0 answers

Publisher-subscriber vs Observer

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 wondering why there are two, similar looking design…
41
votes
5 answers

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 suggest me any tutorial.
vnshetty
  • 20,051
  • 23
  • 64
  • 102
40
votes
12 answers

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 with my user model. I'm setting the activation…
40
votes
5 answers

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 be changing) and I want the ActivityList to be in…
rhinds
  • 9,976
  • 13
  • 68
  • 111
40
votes
2 answers

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
  • 30,868
  • 62
  • 166
  • 261
39
votes
7 answers

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 using jquery and javascript.
amateur
  • 43,371
  • 65
  • 192
  • 320
38
votes
6 answers

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 Callback{ public void notify(MethodResult…
DD.
  • 21,498
  • 52
  • 157
  • 246
37
votes
1 answer

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 elaborate. Thanks!
thomasfedb
  • 5,990
  • 2
  • 37
  • 65
37
votes
5 answers

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. This should explain my intentions: struct A { …
VP.
  • 15,509
  • 17
  • 91
  • 161