I have 3 observers of a subject that need to be notified when the subject state changes, but:
based on the value of the changed state every observer could do something or not.
i think that there are two ways:
- Consider an observer a class that it's interested to the state (regardless of the value) and the observer knows if it needs to do something with that specific state
- Consider an observer a class that it's interested to specific state values of an object (those for which it needs to do something)
If i choose the first option i need to register all the potentially observers and when the state of the subject changes i notify only the observers that are interested (this logic should be encapsulated); maybe i can notify all and delegate the observers to decide if, based on the state value, they need to do something or not
if i choose the second option i need to register and deregister the observers every time the state change based on the state value
Then, who is an observer?
anyone that when notified needs to do something or anyone that it's interested to the changes of the state of a subject but when notified it could do nothing?
how can you solve this situation?
Aucun commentaire:
Enregistrer un commentaire