What's the actual observer pattern from below 2 variations.
- In the first one, the Observer takes the responsibility of subscription.
- But in the second scenario, Publisher takes the responsibility to add a particular observer to the subscriber list.
Because, there are some implementations in the web, that take these both as observer pattern in java.
1.
// Observer is taking the responsibility of subscribing
// Observer.java
@Override
public void subscribe(MessagePublisher publisher) {
publisher.getObservers().add(this);
}
2.
// Publisher is taking the observer to subscribe
// Publisher.java
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
Aucun commentaire:
Enregistrer un commentaire