Closed baskargopinath closed 6 months ago
It is the observers that must implement the interface. Because when the observed executes an event, it must trigger a notify function for the observers accordingly
can u give example @tankh99
Take this example of an observer-observed pair in AB3.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
Here, the CommandBox
is the observer and the TextField commandTextField
is the observed object. Notice that in order for the observer to work, it need to pass an object implementing a specific interface to addListener
, specifically the ChangeListener
interface.
Take this example of an observer-observed pair in AB3.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
Here, the
CommandBox
is the observer and theTextField commandTextField
is the observed object. Notice that in order for the observer to work, it need to pass an object implementing a specific interface toaddListener
, specifically theChangeListener
interface.
As mentioned here @bgopi23
Isn't it also true that the Observed
must implement some sort of addObservers()
and notifyObservers()
methods? That sounds to me like having to implement an interface.
edit: I know the 'correct' answer is True according to the textbook, but I'm wondering why the Observed
is not required to implement an Observable
interface as well)
thanks @E0735389 @tankh99
Isn't it also true that the
Observed
must implement some sort ofaddObservers()
andnotifyObservers()
methods? That sounds to me like having to implement an interface.edit: I know the 'correct' answer is True according to the textbook, but I'm wondering why the
Observed
is not required to implement anObservable
interface as well)
@dillontkh The correct answer is False, as the statement given in the question is not entirely correct.
Yes, the observed must also provided a way for observers to register. But this is not as prominent as observables dictating an interface for all observers to implement. It is also not that fixed e.g., observers can be supplied to the observable through its constructor, instead of through a separate method.
Notifying can be an internal mechanism not visible to outsiders.
Why is this false?