Complete the code to declare the Observer interface method.
interface Observer {
void [1]();
}The Observer interface defines an update method that subjects call to notify observers of changes.
Complete the code to add an observer to the subject's list.
class Subject { private List<Observer> observers = new ArrayList<>(); public void [1](Observer o) { observers.add(o); } }
The method addObserver is commonly used to add observers to the subject's list.
Fix the error in the notifyObservers method to correctly call update on each observer.
public void notifyObservers() {
for (Observer o : observers) {
o.[1]();
}
}The update method is the standard method observers implement to receive notifications.
Fill both blanks to complete the Subject class with observer management methods.
class Subject { private List<Observer> observers = new ArrayList<>(); public void [1](Observer o) { observers.add(o); } public void [2](Observer o) { observers.remove(o); } }
The addObserver method adds an observer, and removeObserver removes one from the list.
Fill all three blanks to complete the Observer pattern update flow.
interface Observer {
void [1]();
}
class Subject {
private List<Observer> observers = new ArrayList<>();
public void [2](Observer o) {
observers.add(o);
}
public void notifyObservers() {
for (Observer o : observers) {
o.[3]();
}
}
}The Observer interface has the update method. The Subject adds observers with addObserver. When notifying, it calls update on each observer.
