I'm modelling a situation in which there are:
now I would make a piece of diagram in which using observer pattern describe the fact that each list implement different type of notify() (for example some change in the state of a list need to be notified only to some observer, with some criterion)
I made something like:
in this case each subject override the notify method in order to notify only some subset of observer depend on some criterion, and for using the correct update method.
ListaMDDpubblico is a list composed by some file, each file have a specific tag. When a file is loaded, only the notificationBox associated at the user that "like" the tag of the file should be notified by using updateMDD.
It is [GoF]-friendly?
Or I need to make 3 different Subject abstract class, each implement the notify method in the list-way?
Thanks in advance
After some reasoning on the answer and comment, another possible design of this situation that I made is:
In this way each change is notify at all subscribed observer (for each different type of subject) and logic to understand if the notification must be taken into consideration is modeled in the update methods implemented by the notificationBox (so the notification now is broadcast and each ConcreteSubject doesn't need to know nothing about concreteObserver).
In the GoF observer, notify()
is implemented in the abstract Subject
: the update()
function of all observers are called, up to them to decide if the object-update notification is relevant or not. In this way, the subject does not have to know anything specific about the observers.
If you let Subject
decide which Observer
to notify, the subject might need to know additional details about the observer. Depending on what the subject needs to know about the observer for its decision making, this may or may not be ok:
notify()
be general, and make it dependent on an abstract condition that can change according to the concrete subject. It seems that your concrete observers need to know the type of the subject in order to call the right update function. I'm not sure that it's really the case, but it's the impression coming your naming convention of updateXXX()
, because each XXX
is used in ony one subject.
If this is the case, the Observer
abstraction would depend on concrete implementation of Subject
. This does not seem a good idea: concrete classes may depend on abstract classes, but the contrary is against the open/close principle.
On the UML diagram, I would advise not to use the black composition diamond from Subject
to Observer
:
The navigable association from concrete observer to all the concrete subjects raises questions:
Think about the open/close principle in this regard. What would you expect to happen if you'd need to add a new concrete subject ? Would you have to change all the concrete observers (adding a new association) ? Or would you expect it to work without any change (because the association is with the abstract subject) ?