LogReport is an Observer that subscribes to BaseballReporter and immediately prints each received baseball event as a numbered game-hits log. section 12.1.3 section 12.1.2
Its Observer-based implementation separates reporting from event production:
- Registers itself with BaseballReporter in its constructor. section 12.1.3
- Implements update() as the required Observer operation. section 12.1.3
- Retrieves the current Event through the reporter and translates its hit value into text such as an out, single, double, triple, or homer. section 12.1.3 section 12.1.2
- Maintains a count and prints each player name and result immediately. section 12.1.2
Baseball event logging
Constructing LogReport(reporter) prints the GAME HITS LOG heading and subscribes the object. Each notification then produces a line such as a numbered player hit description while GraphReport and TableReport process the same event independently. section 12.1.3 section 12.1.2
Figure 12.3 This version of the application is modeled from the Observer Design Pattern. The content publisher BaseballReporter is a subclass of Subject, and each report class implements interface Observer. Member variable observers of class Subject is a vector of subscribed observers. Each time BaseballReporter uses EventManager to create a new Event object, it points private member variable current_event to the object. Then it calls the notify() member function of superclass Subject, which iterates over the observers vector and calls the update() member function of each subscribed subscriber. Each subscriber can then decide whether to call member function get_current_event() to get the current Event object. The grayed-out portions of the diagram have not changed logically from figure 12.2.
Shows LogReport within the publisher-subscriber flow: BaseballReporter creates an Event, Subject notifies observers, and LogReport retrieves the current event. section 12.1.3
