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
24
votes
5 answers

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() { @Override public void run() { //…
MBH
  • 16,271
  • 19
  • 99
  • 149
24
votes
2 answers

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 to use EventHandler instead of a delegate? I'm not…
Hsu Wei Cheng
  • 285
  • 1
  • 3
  • 10
24
votes
6 answers

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 without ugly casts. In fact, in "How do I know the…
hcarver
  • 7,126
  • 4
  • 41
  • 67
23
votes
5 answers

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 monitors several subjects and responds when any one of…
blueshift
  • 831
  • 2
  • 12
  • 24
21
votes
4 answers

How to create a full Audit log in Rails for every table?

We recently began a compliance push at our company and are required to keep a full history of changes to our data which is currently managed in a Rails application. We've been given the OK to simply push something descriptive for every action to a…
Mark S.
  • 1,082
  • 1
  • 12
  • 22
21
votes
6 answers

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
  • 7,830
  • 2
  • 41
  • 42
20
votes
6 answers

How is SplSubject/SplObserver useful?

The Standard PHP Library includes what some resources call a reference implementation of the Observer pattern, by way of the SplSubject and SplObserver classes. For the life of me, I can't figure out how these are very useful with no way to pass…
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
20
votes
3 answers

Alternative to Java's Observable class?

I'm coming to Java from the C# world where the Observer pattern is implemented as a first-class language construct with the event keyword. I see that Java's had the Observable class since the early days, but it clearly has implementation issues and…
HolySamosa
  • 9,011
  • 14
  • 69
  • 102
19
votes
5 answers

Good way to have a Collection Listener?

Is there a better way to have a listener on a java collection than wrap it in a class implementing the observer pattern ?
wj.
  • 2,001
  • 3
  • 18
  • 19
18
votes
6 answers

Implementing Notifications in Rails

In my application, I want to notify a user, when he/she is mentioned in a comment or a post. The user handle is @user_name, similar to Facebook. The database table for mentions looks like: Mention mentioned_by: user_id (foreign key) …
18
votes
1 answer

Is Flow API replacing Observer and Observable

In Java 9 does Flow API replace Observer and Observable? If not, what does?
Denis Murphy
  • 1,137
  • 1
  • 11
  • 21
18
votes
2 answers

Observer pattern in Go language

This problem is pretty common: an object should notify all its subscribers when some event occurs. In C++ we may use boost::signals or something else. But how to do this in Go language? It would be nice to see some working code example where a…
Stas
  • 11,571
  • 9
  • 40
  • 58
18
votes
10 answers

how to implement observer pattern in javascript?

Hi I'm tyring to implement observer pattern in JavaScript: My index.js: $(document).ready(function () { var ironMan = new Movie(); ironMan.setTitle('IronMan'); ironMan.setRating('R'); ironMan.setId(1); // ironMan.setCast(['Robert Downey…
Ignacio Garat
  • 1,536
  • 6
  • 24
  • 48
18
votes
5 answers

How do you determine if an object (self) with a lot of properties has been changed?

The short version of the question: I have a class with a ton of declared properties, and I want to keep track of whether or not there have been any changes to it so that when I call a save method on it, it doesn't write to the database when it isn't…
lnafziger
  • 25,760
  • 8
  • 60
  • 101
17
votes
4 answers

iPhone : camera autofocus observer?

I would like to know if it's possible to receive notification about autofocus inside an iPhone application? I.E, does it exist a way to be notified when autofocus starts, ends, if it has succeed or failed... ? If so, what is this notification name ?
Sly
  • 2,105
  • 5
  • 22
  • 28