jeudi 24 août 2023

Observer Pattern - notification with multiple values [closed]

In my application, I'm using the observer pattern for updating the user interface (GUI), logging measured values to a file, and so on. The method that retrieves new values returns a dictionary like {'x': x_value, 'y': y_value, ...}.

I want to implement safety functions that will verify whether the values meet certain conditions, and if so, trigger appropriate emergency actions.

For this purpose, I've created an Alarm class that takes a condition and a reaction as two methods. Such alarms are attached to the relevant events in the subject.

I'm wondering which approach would be better: a) Notifying about each value separately, which would allow defining the alarm condition as something like lambda value: value > 20. This solution seems straightforward but lacks flexibility. b) Notifying about all measurements at once and ignoring values that are not of interest, like lambda *_, value = None, **_: value is not None and value > 20. This approach is more flexible as all alarms are attached to one event, but it might raise some concerns.

Or maybe there's another way?

Aucun commentaire:

Enregistrer un commentaire