mardi 2 juin 2020

Pattern Observer and Spring

I use Spring. I implemented the pattern Observer as shown in the examples on the Internet.

@Component
public class ObserverManager {

    private Map<String, List<IObserver>> observers = new HashMap<>();

    public void subscribe(String type, IObserver observer){
        // do something
    }

    public void notifyObserver(String type, Object data){
        observers.get(type).forEach(observer -> observer.update(data));
    }

    // more
}
@Component
public class NewsObserver implements IObserver {
    @Override
    public void update(Object o) {
        //do something
    }
}

But I don't understand how to correctly add an observer to ObserverManager. It can be injected via the constructor. But if the observers are 7-8? This is a lot for the constructor.How to solve this problem correctly?

Aucun commentaire:

Enregistrer un commentaire