Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/imperative-programming-resit/8/d/solution

The Observer pattern participants are:
  • Service: the subject abstraction, owning the current value and registered Company references, and providing registration and notification operations.
  • SomeService: the concrete subject, making updateValue notify after establishing the new subject state.
  • Company: the observer object interface, declaring the meth() callback.
  • PrivateCompany and PublicCompany, together with the extension NonProfit: concrete observers, registering themselves, retaining their subject, and updating their own values on notification.
The printed PrivateCompany starts with factor but uses factor in its callback; PublicCompany starts with factor but later uses . These are the actual source behaviors, not a uniform pricing rule inferred from their names. The subject stores a list and prepends new subscriptions, so notification occurs in reverse registration order. Dependence on the Company interface lets the subject notify new observer types without naming their concrete classes. The omitted ... portions in the printed figure do not prevent identifying these roles or implementing the added observer.

New to topics? Read the docs here!