Observable in Java

前端 未结 9 1847
别那么骄傲
别那么骄傲 2020-12-14 19:30

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         


        
相关标签:
9条回答
  • 2020-12-14 20:10

    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.

    0 讨论(0)
  • 2020-12-14 20:11

    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:

    1. A observer (listener, IntegerAdder) adds itself to an observable (IntegerDataBag)
    2. Something happens at the observable and the observable notifies its observers
    3. The observable will call the callback methods of all actual observers
    4. The observer now has been notified of the event and can use it

    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.

    0 讨论(0)
  • 2020-12-14 20:16
    1. Yes. addObserver is a method in the Observable abstract class. See Observable in the Java documentation.
    2. It is added to a list in Observable.
    3. A call to notifyObservers will do nothing until setChanged is set.
    4. You might have multiple Observables in the same application.
    5. 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.
    0 讨论(0)
提交回复
热议问题