Complete the code to declare the Observer interface method.
interface Observer {
void [1]();
}The Observer interface requires an update method that the Subject calls 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 to add an observer is often called registerObserver to clearly indicate the action.
Fix the error in the Subject's notify method to call update on all observers.
public void notifyObservers() {
for (Observer o : observers) {
o.[1]();
}
}The Subject calls the update method on each observer to notify them of changes.
Fill both blanks to complete the Subject's state change and notify flow.
public void setState(String state) {
this.state = [1];
[2]();
}The state is set to the new value passed in, then notifyObservers() is called to update all observers.
Fill all three blanks to complete the Observer's update method using the Subject's state.
class ConcreteObserver implements Observer { private Subject subject; private String observerState; public ConcreteObserver(Subject subject) { this.subject = subject; } public void update() { observerState = subject.[1](); System.out.println("Observer state updated to: " + [2]); [3](); } private void refreshUI() { // Code to refresh UI } }
The observer calls getState() on the subject to get the latest state, updates its own observerState, then calls refreshUI() to update the user interface.