R
Rogan Dawes
Hi,
I am trying to design a web security application.
I am trying to separate the model and the controller(s) and the UI, as
is good practice.
So, it seems that the way to do this is to have the Model extend
Observable, and have the various controllers Observe it.
I plan to have multiple plugins (controllers), which all Observe the model.
As part of their observeration, they might make further changes to the
model, which will also trigger updates/notifications.
How do I prevent recursive notification from happening, and make sure
that all Observers get their notifications in the right order?
Stupid example:
A temperature controller app.
Model:
Keeps track of the temperature, issues updates if it changes.
Also keeps track of the state of the heater and the aircon, and issues
updates if those change.
Controller:
Reads the temperature from a sensor, and updates the model.
Observer1:
A display that reports the temperature, and the status of the aircon and
heater
Observer2:
A feature that automatically turns on the aircon or heater if the
temperature goes out of limit.
So the process would go something like:
* Controller reads the temperature, updates the model.
* Model calls update(temperature) in each Observer sequentially. Say it
calls Observer2 first.
* Observer2 notices that the temperature is too high, and turns on the
aircon, and updates the model to reflect this.
* The model then calls update(aircon) in each Observer sequentially.
At this point, we are re-entering Observer2.update() recursively.
In this stupid example, this may not be such a big deal, but now add
another Observer that is tracking the status of the aircon, and turning
off the heater whenever the aircon comes on. (I know, bad practice, it
should have been turned off long ago. Its a stupid example, OK?!)
So the Observer2 update(aircon) finishes, Observer1 update(aircon)
finishes, and the Observer3 update(aircon) is called, which turns off
the heater, and updates the model.
The model then calls update(heater) in each observer sequentially.
Note that, at this point, the Observer1 has still not received the
initial temperature notification, although it has received the aircon
notification.
This all seems wrong to me. The question is, how to fix it?
1. Make the model queue update() events, and fire them sequentially,
making sure that each one is finished before firing the next.
Pro: No more recursion.
Con: The first invocation of a model changing method has to loop
repeatedly (inside the model), and will only return after any resulting
events have been dispatched.
Con: Possible deadlock/synchronising problems?
Con: What happens if another thread submits an update while the queue is
being processed? That thread would return immediately, and the original
thread then has to deal with those updates too?
2. Give the model an event thread, and allow each update to return
immediately, while the event thread calls update for each event
sequentially.
Pro: model updates return immediately, and the caller can go back to
what it was doing.
Con: Does having an event thread violate the MVC design?
Other approaches?
Rogan
I am trying to design a web security application.
I am trying to separate the model and the controller(s) and the UI, as
is good practice.
So, it seems that the way to do this is to have the Model extend
Observable, and have the various controllers Observe it.
I plan to have multiple plugins (controllers), which all Observe the model.
As part of their observeration, they might make further changes to the
model, which will also trigger updates/notifications.
How do I prevent recursive notification from happening, and make sure
that all Observers get their notifications in the right order?
Stupid example:
A temperature controller app.
Model:
Keeps track of the temperature, issues updates if it changes.
Also keeps track of the state of the heater and the aircon, and issues
updates if those change.
Controller:
Reads the temperature from a sensor, and updates the model.
Observer1:
A display that reports the temperature, and the status of the aircon and
heater
Observer2:
A feature that automatically turns on the aircon or heater if the
temperature goes out of limit.
So the process would go something like:
* Controller reads the temperature, updates the model.
* Model calls update(temperature) in each Observer sequentially. Say it
calls Observer2 first.
* Observer2 notices that the temperature is too high, and turns on the
aircon, and updates the model to reflect this.
* The model then calls update(aircon) in each Observer sequentially.
At this point, we are re-entering Observer2.update() recursively.
In this stupid example, this may not be such a big deal, but now add
another Observer that is tracking the status of the aircon, and turning
off the heater whenever the aircon comes on. (I know, bad practice, it
should have been turned off long ago. Its a stupid example, OK?!)
So the Observer2 update(aircon) finishes, Observer1 update(aircon)
finishes, and the Observer3 update(aircon) is called, which turns off
the heater, and updates the model.
The model then calls update(heater) in each observer sequentially.
Note that, at this point, the Observer1 has still not received the
initial temperature notification, although it has received the aircon
notification.
This all seems wrong to me. The question is, how to fix it?
1. Make the model queue update() events, and fire them sequentially,
making sure that each one is finished before firing the next.
Pro: No more recursion.
Con: The first invocation of a model changing method has to loop
repeatedly (inside the model), and will only return after any resulting
events have been dispatched.
Con: Possible deadlock/synchronising problems?
Con: What happens if another thread submits an update while the queue is
being processed? That thread would return immediately, and the original
thread then has to deal with those updates too?
2. Give the model an event thread, and allow each update to return
immediately, while the event thread calls update for each event
sequentially.
Pro: model updates return immediately, and the caller can go back to
what it was doing.
Con: Does having an event thread violate the MVC design?
Other approaches?
Rogan