Observable in Java

前端 未结 9 1846
别那么骄傲
别那么骄傲 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 19:52

    The update() method is called by the Observable. This method is called by calling notifyObservers(), which itterates trough all Observers and calls update on them. This informs the Observer that the object they are watching has been changed and a certain action can be performed. The args object is whatever you want it to be, it can be null aswell but it can be used to inform the observers what type of update just took place.

    0 讨论(0)
  • 2020-12-14 19:55

    The bag.addObserver() can be made only because IntegerDataBag extends Observable?

    Yes, the addObserver method is implemented in Observable.

    Where is this observer being add to? what being created and where?

    The observer is being added to a list of observers which is declared in Observable as private, so it is not visible in your subclass.

    What is the different between setChanged() and notifyObservers()?

    When you call notifyObservers() without first calling setChanged(), no notifications take place.

    I don't understand the update method- what does args stands for? and why do I need to check that o==bag, why would I update another observable?

    One Observer can watch multiple Observables. By examining the first parameter of the update method, you can figure out which observer is notifying you about something.

    Why should I need this observer anyway?

    Any time you want a class to send events to other classes but you don't want a direct dependency from that class on its observers.

    0 讨论(0)
  • 2020-12-14 19:59

    The observer pattern is similar to the concept of listeners. The object which is being listened to maintains a record of all of it's listeners. For instance, a stock monitor class may allow objects to listen for a certain event, such as the stock level falling below the warning level.

    From the observer's point of view:

    Call subscribe() or addEventListener()or the like. The observer is then "notified" when the event actually occurs, usually by means of calling a function in the observer (the event handler function).

    from the observable's point of view:

    Objects wishing to observe the observable object register their interest by calling the subscribe() or addEventListener() as above. The observable thus adds these observers to an array, list, or some other data structure.

    Once the event actually happens, the listeners are notified by calling the event handler function in the observers' class.

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

    In answer to your points.

    1. Yes you're correct

    2. The observer is added to a list maintained in the Observable object

    3. You need to call setChanged() before you notify observers, otherwise they won't know the object has changed. Once you call notifyObservers(), All obvserers are notified of the change. If you don't call setChanged first, your observers won't be notified.

    4. arg is anything you'd like to pass to Observers when you call notifyObservers(arg);

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

    The bag.addObserver() can be made only because IntegerDataBag extends Observable?

    Correct, Observable is a class that has the addObserver() method.

    Where is this observer being add to? what being created and where?

    The Observer class contains a List or Vector (in the JDK source) of Observable objects.

    private Vector obs;
    

    That's where it's stored.

    What is the different between setChanged() and notifyObservers()?

    The setChanged() just marks the Observable is changed. The notifyObservers() just calls the all observers it has on the list to update() (passing a changed object to the Observer) only if the setChanged is set to true.

    I don't understand the update method- what does args stands for? and why do I need to check that o==bag, why would I update another observable?

    The update() method tells the Observer that it needs to update based on the changed obj it receives. This is called by the notifyObservers() from Observable.

    Why should I need this observer anyway?

    To create a Listener for event driven scenario, i.e. if you want to be informed in the change of your observable objects, then Observer is needed.

    Read: GoF - Observer pattern.

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

    The bag.addObserver() can be made only because IntegerDataBag extends Observable?

    Yes.

    2.Where is this observer being add to? what being created and where?

    Into the related Observable class, which your class is extending.

    4.I don't understand the update method- what does args stands for?

    It's called when the state of an observed object has changed. The args is the parameter passed to nofityObserver.

    and why do I need to check that o==bag, why would I update another observable?

    You don't need to check anything.

    5.Why should I need this observer anyway?

    It's a very useful design pattern. Have a look to wikipedia article.

    EDIT: I missed point :

    What is the different between setChanged() and notifyObservers()?

    setChanged() marks an object signalling that it changed. notifyObservers is responsible to wake all observer listening to the observable object.

    0 讨论(0)
提交回复
热议问题