jeudi 5 janvier 2017

Observer pattern with a filter, at what level to filter?

Assume I have a class broadcaster to broadcast certain events. Eg:

List<Observers> observers = …

Public void broadcast(Event e) {
    for (each observer: observers) {
       observer.observe(e);
   }
}

Then I have a class called EventReceiver which does the following

public void processEvent(Event e) {
    saveToDatabase(e);
    broadcast(e);
}

Now: a new requirement comes up, saying that when Event is of type “foo”, we should save it to database but not broadcast it.

At which layer should I filter ? At broadcast.java or in EventReceiver.java ?

Eg:

Public void broadcast(Event e) {
If (event != foo) {
    for (each observer: observers) {
       observer.observe(e);
   }
 }
}

Or

public void processEvent(Event e) {
    saveToDatabase(e);
   If (event != foo) {
        broadcast(e);
   }
}

Aucun commentaire:

Enregistrer un commentaire