0

Hello I am trying to implement an analog clock using the Observer design pattern. I have understood the theory of how it works but when I was coding I couldn't understand the difference between notifyObserver (Observable) and update (Observer), more specifically: when and where do we use notify versus when and where do we us update? They seem to have the same purpose of letting know the observes that something in the program has changed, but they do it differently, which is what I don't quite understand.

Also, I haven't understood very well where the addObserves method needs to be put in order for it to observe.

JavaStudentPanic
  • 47
  • 1
  • 2
  • 10
  • Welcome - to improv e your question, it would be useful to state which "reference" of the Observer design pattern you're using, including code. Java has an implementation, but there are other references that even change `notify` and/or `update` (which makes it confusing). Java also has Listeners that are also observers, but they don't use any of those methods. Finally, I don't think a clock is a good problem to use Observer, since it updates regularly and Observer is more about asynchronous updates. That is, a subject changes spontaneously and observers of it want to know about that. – Fuhrmanator Apr 14 '21 at 18:48

1 Answers1

0

when and where do we use notify versus when and where do we us update

notifyObservers is an Observable method which has the responsibility to iterate through all the observers registered with it and call their individual update method. update method belongs to the Subjects which contain the actual logic of what needs to be done for individual Subjects when the notification happens. This can be different for different subjects. You can read more here on this subject.

Shailendra
  • 8,874
  • 2
  • 28
  • 37