jeudi 27 juin 2019

Variation of Java observer pattern or Publisher-Subscriber Pattern

What's the actual observer pattern from below 2 variations.

  1. In the first one, the Observer takes the responsibility of subscription.
  2. 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