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
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.