Observer Design Pattern - SENG-350-2024-fall/Team-1 GitHub Wiki

Source: app/backend/patient_queue.py, Lines 12-62.

Initializing an empty list to store the observers (in this case the observers are patients, who are observing the queue).

    def __init__(self):
        self.queue = []
        self.observers = []  # Observer Design Pattern

Adding patients to the observer list as they are inserted into the queue and the queue is built from the database.

    for p in queue_db.read_all():
        patient = Patient(p_info=p)
        queue.insert(int(patient.q_pos), patient)
        observer.append(patient)

When a patient is added to the queue they are automatically registered as an observer, and patients observe their updated positions in the queue.

    patient.q_pos = insert_position
    queue_db.add_line(patient)
    self.queue.insert(insert_position, patient)
    self.add_observer(patient)  # Automatically add patient as observer
    self.update_queue_positions()
    self.notify_observers()
    return insert_position

The Observer design pattern is implemented in the PatientQueue class to keep all patients in the queue informed of any updates, such as position changes. Each patient in the queue acts as an observer and receives a notification whenever the queue is modified (e.g., when a patient is added or removed). This approach ensures that all patients have real-time information on their status in the queue without needing to query for updates individually. The Observer pattern is a good choice here because it provides an efficient, centralized way to notify multiple observers automatically, making the system responsive and keeping patients informed in a dynamic, high-priority environment.