nus-cs2103-AY2324S2 / forum

16 stars 0 forks source link

Quiz 11 : Q17 #906

Closed baskargopinath closed 6 months ago

baskargopinath commented 7 months ago

image

Why is this false?

tankh99 commented 7 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

baskargopinath commented 7 months ago

can u give example @tankh99

E0735389 commented 7 months ago

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.

tankh99 commented 7 months ago

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.

As mentioned here @bgopi23

dillontkh commented 6 months ago

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)

baskargopinath commented 6 months ago

thanks @E0735389 @tankh99

damithc commented 6 months ago

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)

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