I\'m trying to understand the Observer and the Observable.
Here\'s an example that I\'m trying to figure out:
public class IntegerDataBag extends Obs
The bag.addObserver() can be made only because IntegerDataBag extends Observable?
Yes, that's the whole point.
Where is this observer being add to? what is being created and where?
It adds IntegerAdder
to the list of classes that observe IntegerDatabag
for changes. From now on, if a change occurs on IntegerDataBag
, it will notify IntegerAdder
via the notifyObservers()
method, which will trigger its update()
method.
What is the difference between setChanged() and notifyObservers()?
notifyObservers()
calls the update()
method of every observer of your observable, and is used to pass infos to this method. As for setChanged()
, it marks your object as "changed" so that the hasChanged()
method will now return true... It's used to monitor changes to your observable class.
I don't understand the update method- what does args stands for?
The update()
method is inherited from the implementation of the observer interface - You have to implement it. The Object arg
is an optional argument that you can pass to the method via notifyObservers()
.
Why do I need to check that o==bag, why would I update another observable?
Since an observer can "observe" more than one "observable", you need to check that it's really IntegerDatabag
that triggered update()
, hence o==bag
.
Why should I need this observer anyway?
You need the observer to monitor IntegerDataBag
for changes. In your case, you print a message in the console when IntegerDatabag
is modified. The purpose of the Observer/Observable model is specifically to monitor changes on specific objects, and then update the program based on the changes.
Let's take a practical example for Observer pattern: Twitter. With Twitter we can follow some other people and read whatever they post in near realtime.
Every twitter user is observable. You can add yourself as a listener ("Follower") and read his/her posts. Every twitter user will do a "notify Followers" (notifyObservers
).
Here we do the same. The class IntegerDataBag
has the capability to notify other classes whenever a value is added to or deleted from it's internal bag. Any instance (that implements Observer
) can register itself to an IntegerDataBag
and will receive messages through it's callback method (update
).
In short, we do the following:
IntegerAdder
) adds itself to an observable (IntegerDataBag
)Hope, this short description helps in understanding this pattern.
publish/subscribe is a similiar pattern: a publisher is like an observable, a subscriber like an observer. And another practical example: one can subscribe to a newspaper and the publisher will send the paper until you cancel your subscription.
addObserver
is a method in the Observable
abstract class. See Observable in the Java documentation.Observable
. notifyObservers
will do nothing until setChanged
is set.Observer
is a common design pattern. The usual example is when you have a Model and multiple Views. Each View is an Observer on the Model; if the Model changes, the Views get updated.